Home >>C++ Standard Template Library Tutorial(STL) >C++ Vector

C++ Vector

C++ Vector

A sequence container class which is known to implement the dynamic array, in simple words that means; the size changes automatically while the elements are appended is known as vector in C++. The elements are stored in contiguous memory locations by the vector and are generally allocated by the memory as per the requirement while run time.

Difference between vector and array

While the array generally follows the static approach that simply mean; the size of it cannot be changed at the run time and on the other hand the vector in C++ implements the dynamic array in simple words it means that the resizing of the appending elements are done automatically by itself.

Consider a vector 'v1'. Syntax would be:

vector v1; 

Here is the example of vector that will help you understand it from a greater depth:

#include<iostream>  
#include<vector>  
using namespace std;  
int main()  
{  
vector<string> vect;  
vect.push_back("cpp STL ");  
vect.push_back("tutorial");  
for(vector<string>::iterator itr=vect.begin();itr!=vect.end();++itr)  
cout<<*itr;  
return 0;   
}  
Output :cpp STL tutorial

C++ Vector Functions

Function Description
at() This function generally delivers a reference to an element.
back() This function generally provides a reference to the last element.
front() This function generally provides a reference to the first element.
swap() This function generally known to exchange the elements between two vectors.
push_back() This function generally adds a new element at the end.
pop_back() This function generally removes a last element from the vector.
empty() This function generally used to determine whether the vector is empty or not.
insert() This function generally insert a new element at the specified position.
erase() This function generally deletes the specified element.
resize() This function generally modifies the size of the vector.
clear() This function generally removes all the elements from the vector.
size() This function generally determines a number of elements in the vector.
capacity() This function generally determines the current capacity of the vector.
assign() This function generally assigns new values to the vector.
operator=() This function generally assigns new values to the vector container.
operator[]() This function is used to access a specified element.
end() This function generally refers to the past-lats-element in the vector.
emplace() This function generally inserts a new element just before the position pos.
emplace_back() This function generally used to insert a new element at the end.
rend() This function generally used to point the element preceding the first element of the vector.
rbegin() This function generally used to point the last element of the vector.
begin() This function generally used to point the first element of the vector.
max_size() This function generally used to determine the maximum size that vector can hold.
cend() This function generally used to refer to the past-last-element in the vector.
cbegin() This function generally used to refer to the first element of the vector.
crbegin() This function generally used to refer to the last character of the vector.
crend() This function generally used to refer to the element preceding the first element of the vector.
data() This function generally used to write the data of the vector into an array.
shrink_to_fit() This function generally used to reduce the capacity and makes it equal to the size of the vector.