STL - Vector - HackerEarth
STL - Vector - HackerEarth
STL - Vector - HackerEarth
LIVE EVENTS
Notes
STL - Vector
2 Vector C++ STL
Vector is one of various useful containers defined under Standard Template Library(STL).
We can say that Vector is an advanced version of array in C++ . Though it contains some
good features than array.
#include<vector>
vector<data_type>identifier(size,initial_value);//Heresize&
initial_valueareoptional
Example :
vector<int>A(5,1);//vectorofsize=5withallelementsinvector
initialisedwithvalue=1
Insertion in Vector , There are two ways for inserting an element in a Vector , either you
can use push_back() or insert(index,value) , insert() will insert value at index give ,
and push_back() will insert value after the last element which is already in vector. And
insert() will insert the value as well as it will swap all the preceding elements by the
number of elements inserted
Example :
A.push_back(5);//A={1,1,1,1,1,5},
vectorcanexpanditssize
https://www.hackerearth.com/notes/stlvector/ 1/4
8/17/2016 STLVector|HackerEarth
A.insert(1,6);//A={1,6,1,1,1,1,5}
A.resize(10);//nowthevector'A'hassize=10
A.erase();//EraseallvaluesofvectorA
A.erase(1,5);//Erasevaluesfromindex=1to5
if(A.empty()){//Statements}
Importing value of any array into a vector , let an array array like
intarray[]={1,2,3,4,5};
vector<int>A(array,array+5);//Itwillmakevectorelements
A={1,2,3,4,5}
Size of vector :
intsize=A.size();//itgivessizeofvector'A',i.e.numberof
elementspresentinvector'A'
vector<int,vector<int>>Mat(M,vector<int>N);//Matrixofsize
M*N
Many <algorithm> library function can be implemented on vector(s) , that I will explain
in <algorithm> Note
https://www.hackerearth.com/notes/stlvector/ 2/4
8/17/2016 STLVector|HackerEarth
Tweet
COMMENTS (0)
Login/Signup to Comment
AUTHOR
Bhavesh Kumar
Problem Setter at HackerE
Jaipur
1 note
TRENDING NOTES
https://www.hackerearth.com/notes/stlvector/ 3/4
8/17/2016 STLVector|HackerEarth
Competitive Programming
Open Source
EMPLOYERS REACH US
Developer Sourcing
Lateral Hiring
Campus Hiring
Hackathons
contact@hackerearth.com
+91-80-4155-4695
+1-650-461-4192
2016 HackerEarth
https://www.hackerearth.com/notes/stlvector/ 4/4