Vectors are similar to dynamic arrays in that they may resize themselves dynamically when an element is added or removed, and their storage is handled automatically by the container.
vector Declaration
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v; /// vector1 declatation
vector<int> v2; /// vector2 declatation
push_back()
It push the elements into a vector from the back
int main()
{
vector<int> v; /// vector1 declaration
v.push_back(1); // value input
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
vector<int> v2; /// vector2 declaration
v2.push_back(11);
v2.push_back(12);
v2.push_back(13);
v2.push_back(14);
Print a vector
Type 1:-
vector<int>::iterator it; // iterator declaration
for (it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
Type 2:-
// auto iterator ///
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
front()
cout <<"Front : "<< v.front() << endl;
back()
cout <<"Back :"<< v.back() << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";// print vecter
}
pop_back()
v.pop_back() ;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
size()
cout << "size : " << v.size() << endl;
clear()
v.clear();
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
empty()
cout <<"empty : "<< v.empty() << endl;
// if empty ==0 then true else empty==1 false
insert()
v.insert(v.begin()+5,99);/// insert index and input number
erase()
v.erase(v.begin()+5 );/// erase by index number
reverse()
reverse(v.begin(),v.end()); // first index
reverse(v.begin()+4,v.end()); //with index number
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";// print vecter
}
sort()
sort(v.begin(),v.end()); // first index
sort(v.begin()+4,v.end());// input index number
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";// print vecter
}
swap()
swap(v, v2); /// swap between 2 vectors
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";// print vecter
}
0 Comments