C++ Vector

A vector is a sequence container class that implements dynamic array, means size automatically changes when appending elements. A vector stores the elements in contiguous memory locations and allocates the memory as needed at run time.

Difference between vector and array

An array follows static approach, means its size cannot be changed during run time while vector implements dynamic array means it automatically resizes itself when appending elements.


Consider a vector 'v1'. Syntax would be:

  1. vector<object_type> v1;  


Let's see a simple example.

  1. #include<iostream>  
  2. #include<vector>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6. vector<string> v1;  
  7. v1.push_back("javaTpoint ");  
  8. v1.push_back("tutorial");  
  9. for(vector<string>::iterator itr=v1.begin();itr!=v1.end();++itr)  
  10. cout<<*itr;  
  11. return 0;   
  12. }  


javaTpoint tutorial

In this example, vector class has been used to display the string.

C++ Vector Functions

Function Description
at() It provides a reference to an element.
back() It gives a reference to the last element.
front() It gives a reference to the first element.
swap() It exchanges the elements between two vectors.
push_back() It adds a new element at the end.
pop_back() It removes a last element from the vector.
empty() It determines whether the vector is empty or not.
insert() It inserts new element at the specified position.
erase() It deletes the specified element.
resize() It modifies the size of the vector.
clear() It removes all the elements from the vector.
size() It determines a number of elements in the vector.
capacity() It determines the current capacity of the vector.
assign() It assigns new values to the vector.
operator=() It assigns new values to the vector container.
operator[]() It access a specified element.
end() It refers to the past-lats-element in the vector.
emplace() It inserts a new element just before the position pos.
emplace_back() It inserts a new element at the end.
rend() It points the element preceding the first element of the vector.
rbegin() It points the last element of the vector.
begin() It points the first element of the vector.
max_size() It determines the maximum size that vector can hold.
cend() It refers to the past-last-element in the vector.
cbegin() It refers to the first element of the vector.
crbegin() It refers to the last character of the vector.
crend() It refers to the element preceding the first element of the vector.
data() It writes the data of the vector into an array.
shrink_to_fit() It reduces the capacity and makes it equal to the size of the vector.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #