CPP Vector

A vector in CPP is used to implement a dynamic array. Vector in real, is a sequence container class. Here, dynamic array means that the array size automatically resizes itself when appending elements at run time. However, in static array, array size cannot be changed during run time.

Syntax:

vector <object_type> vector_name;

CPP Vector Functions:

FUNCTION USES
at() To provide a reference to an element.
assign() To assign new values to the vector.
begin() To point to the first element of the vector.
back() To give a reference to the last element.
cend() To refer to the past-last-element in the vector.
cbegin() To refer to the first element of the vector.
capacity() To determine the current capacity of the vector.
clear() To remove all the elements from the vector.
crbegin() To refer to the last character of the vector.
data() To write the data of the vector into an array.
empty() To determine whether the vector is empty or not.
emplace_back() To insert a new element at the end.
erase() To delete the specified element.
end() To refer to the past-last-element in the vector.
emplace() To insert a new element just before the position pos.
front() To give a reference to the first element.
insert() To insert a new element at the specified position.
max_size() To determine the maximum size that vector can hold.
operator=() To assign new values to the vector container.
operator[]() To access a specified element.
push_back() To add a new element at the end.
pop_back() To remove a last element from the vector.
rend() To point to the element preceding the first element of the vector.
rbegin() To point to the last element of the vector.
resize() To modify the size of the vector.
size() To determine number of elements in the vector.
shrink_to_fit()             To reduce the capacity to make it equal to the size of the vector.
swap() To exchange the elements between two vectors.

 

Example:

#include<iostream>  
#include<vector>  
using namespace std;  
int main()  
{  
vector<string> vec;  
vec.push_back("HELLO ");  
vec.push_back("CPP !!");  
for(vector<string>::iterator i=vec.begin();i!=vec.end();++i)  
cout<<*i;  
return 0;   
}

Output

HELLO CPP !!
Please follow and like us:
Content Protection by DMCA.com