Note on Vector(STL)
By UJJAL ROY
1.1 Vector
Vector is a dynamic array/Resizable Array. We can push or pop element at any time That means we can increase or decrease the size of the vector. It not necessary to tell size of the vector when it is declared.
Declaration of vector
vector<type> name;
Here the type can be anything like int, char, string, double etc.
vector<int>v;
vector<string>vs;
vector<char>vc;
vector<vector<char>>vv;
It is allowed to tell size of the vector at the declaration time. Initially all the vector element will be initialized by zero or we can be initialized by any value at the time of declaration
vector<int>v(5); //{0,0,0,0,0}
vector<int>v(5,10); //{10,10,10,10,10
Initialization
vector<int> v = {10, 20, 15, 50};
vector<string> vs = {"Hello","Ujjal"};
Now we can use it like as array. We can access any index like as array
cout<<v[0]<<endl;//output : 10
v[1]=100;
cout<<v[1]<<endl; //output : 100
vector traversing
we can use for loop, while loop, do while loop or For-each loop for traversing vector.
Using for loop
vector<int> v = {10, 20, 15, 50};
for (int i = 0; i < 4; i++)
{
cout << v[i] << endl;
}
Using For-each loop
vector<int> v = {10, 20, 15, 50};
for (auto u : v)
{
cout << u << endl;
}
**Output :**
10
20
15
50
push_back()
For adding an element of a vector in the end we can use push_back() function. The complexity of this function is O(1).
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for (int i = 0; i < 3; i++)
{
cout << v[i] << " ";
}
vector<char> vs; // char type vector
vs.push_back('U');
vs.push_back('J');
vs.push_back('J');
vs.push_back('A');
vs.push_back('L');
for (auto u : vs)
cout << u;
For taking input from user. we will use extra variable of same type. Firstly, we will take input in this variable then we will push it on the vector.
vector<double> vd;
for (int i = 0; i < 5; i++)
{
double r;
cin >> r;
vd.push_back(r);
}
size()
The size() function directly return the size of vector. The time complexity of this function O(1).
vector<int>v={10,20,30,40};
cout<<v.size()<<endl; // Output : 4.
clear()
clear() remove all the element from the vector. So the vector will be empty. Its time complexity is O(1).
vector<int>v={10,20,30,40};
cout<<v.size()<<endl; // Output : 4.
v.clear();
cout<<v.size()<<endl; //output: 0
empty()
empty() is a Boolean function it return 1(True) when vector is empty otherwise it return 0(false). Its time complexity is O(1).
vector<char>vc={'A','B'};
cout<<vc.empty()<<endl; //output : 0
vc.clear();
cout<<vc.empty()<<endl; //output : 1
insert()
for adding new element at any index we use insert() function. It take two parameter one is iterator (mainly it is position) and another is the value of this element.
For example the vector is v={1,2,3,4}
v.insert(v.begin()+1,10);
here v.begin()+1 indicate index v[1]. so the value of index v[1]=10 and all the
element will be shift to the next position that means previously 2 was at v[1] index
now it will be v[2] index 3 will be v[3] and so on.
Now we thing the complexity of this function. Here for begin() iterator all the element will be shift so the complexity of this function is O(n).
then v will be v={1,10,2,3,4}
vector<int>v={1,2,3,4};
v.insert(v.begin()+1,10);//{1,10,2,3,4};
for(auto u:v)cout<<u<<" "; //output : 1 10 2 3 4
erase()
this function almost similar with insert function but insert() add new element to the vector and erase() remove element from vector .
vector<int>v={1,2,3,4,5};
v.erase(v.begin()); //2,3,4,5
for(auto u:v)cout<<u<<" ";
cout<<endl;
v.erase(v.begin(),v.begin()+2);//{4,5};
for(auto u:v)cout<<u<<" ";
resize()
this function changes the size of vector and rest of the element are assign by zero. The Time Complexity comes out to be O(n).
vector<int>v(5,2);
cout<<v.size()<<endl; //output:5
for(int i=0;i<v.size();i++)cout<<v[i]<<" "; //output :{2,2,2,2,2};
v.resize(10);
cout<<v.size()<<endl; //output:10
for(int i=0;i<v.size();i++)cout<<v[i]<<" ";//output
:{2,2,2,2,2,0,0,0,0,0};
reverse()
this function reverse the whole vector. The syntax of the function. The time complexity is O(n).
ector<int>v={1,2,3,4}
reverse(v.begin(),v.end());
now v={4,3,2,1}
front() and back()
front() function return the first element of vector and back() function the last element o this vector. The time complexity of both function is O(1).
vector<long long int>v={10,20,30,40,50};
cout<<v.front()<<" "<<v.back()<<endl; //output 10 50
max_element() and min_element()
this function return the first iterator of maximum element .and the complexity of this function is O(n).
vector<int>v={1,2,3,5,4,1,5};
auto it=max_element(v.begin(),v.end());
cout<<*it<<endl;// output : 5. value of maximum element
cout<<it-v.begin()<<endl; //output : 3. Index of maximum element
the use of min_element() is similar to max_element() function.
Copy of vector
Vector can be copy directly like as primitive variable. When we copy a vector source vector will remain unchanged
vector<char>vc={'U','j','j','a','l'};
vector<char>vc1;
vc1=vc;
for(auto u:vc1)cout<<u; //output:Ujjal
sorting of vector
using sort() function vector can be sort. This is ascending order by default. Using greater comparator it will be descending order or we can use rbeging, rend Iterators
code for ascending order
vector<int>v={4,1,2,3,2,5,1};
sort(v.begin(),v.end());
for(auto u:v)cout<<u<<" ";
// output : 1 1 2 2 3 4 5
Code for descending order
vector<int>v={4,1,2,3,2,5,1};
sort(v.begin(),v.end(),greater<int>());//sort(v.rbegin(),v.rend());
for(auto u:v)cout<<u<<" ";
// output : 5 4 3 2 2 1 1
Great blog!
I was searching for something like this when I was learning STL.
How about cppreference?