# Vector in STL C++

Vector in STL is defined as sequence containers which can store elements and can change its size dynamically during run-time of the program. Vector are also known as dynamic array.

## Syntax of Vector :

```vector< data_type> vector_name;
```

In C++, vectors are not ordered. Iterators make it simple to retrieve and navigate through vector items that are stored in nearby storage.Data is added at the end of vectors when the push back() method is used. As there may occasionally be a need to expand the vector, adding an element to the end of the vector requires differential time, but adding an element to the beginning or centre of the vector just requires linear time. Due to the absence of any scaling, removing the last piece just requires constant time.

### Functions in Vector :

Function Working
begin() gives back an iterator pointing to the vector’s first element.
end() gives back an iterator pointing to the vector’s last element.
size() gives the number of elements in vector
push_back() Add an element in the last of the vector
empty() return whether the vector is empty or not

#### Example of Vector in STL :

Run
```#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
vector< int >vect;
// adding elements to the vector
vect.push_back(2);
vect.push_back(5);
vect.push_back(10);
vect.push_back(12);

//size of vector
cout<< "size of vector "<< vect.size()<< endl;

//printing elements of vector
cout<<"The elements of vector : ";
for(int i=0;i< vect.size();i++){
cout<< vect[i]<<" ";
}

return 0;
}
```

### Output :

```size of vector 4
The elements of vector : 2 5 10 12
```

In the above program, we take the vector vect and display the size and elements of vector

### Example of Vector in STL:

Run
```#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
vector< int>vect;
// adding elements to the vector
vect.push_back(2);
vect.push_back(5);
vect.push_back(10);
vect.push_back(12);

//size of vector
cout<< "size of vector "<< vect.size()<< endl;

//printing elements of vector
cout<<"The elements of vector : ";
for(int i=0;i< vect.size();i++){
cout<< vect[i]<<" ";
}
cout<< endl;
vect.pop_back();
cout<<"The elements of vector after pop_back() ";
for(int i=0; i< vect.size();i++){
cout<< vect[i] << " ";
}
cout<< endl;
// checking whether vector is empty or not
if(vect.empty()){
cout<<"vector is empty";
}
else{
cout<<"vector is not empty";
}

return 0;
}
```

### Output :

```size of vector 4
The elements of vector : 2 5 10 12
The elements of vector after pop_back() 2 5 10
vector is not empty
```

In the above program, we take the vector vect and used different functions like push_back(), empty(), pop_back() to understand its functioning.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others