Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- vector<int>v(5,5);//a vector with n elements and all equal to 5;
- for(int i=0 ; i < 5; i++)
- v.push_back(i+1);
- //PRINT VECTOR
- vector<int>::iterator it;
- for(it=v.begin();it!=v.end();it++)
- cout<<*it<<" ";
- for(auto it:v)
- cout<<it<<" ";
- for(auto it=v.begin();it!=v.end();it++)
- cout<<*it<<" ";
- vector<int>::reverse_iterator itr;
- for(itr=v.rbegin();itr!=v.rend();itr++)
- cout<<*itr<<" ";
- for(auto itr=v.rbegin();itr!=v.rend();itr++)
- cout<<*itr<<" "*/
- //REVERSE PRINT VECTOR
- vector<int>::iterator it1=v.end();
- it1--;
- for(;it1>=v.begin();it1--)
- cout<<*it1<<" ";
- while(it1!=v.begin()){
- it1--;
- cout<<*it1<<" ";
- }
- if(v.empty())
- cout<<"vector is empty"<<" "<<v.empty();//value of v.empty()=1;
- else
- cout<<"not empty"<<v.empty()<<" ";//value of v.empty()=0;
- cout<<v.front()<<" "; //first element of vector
- cout << v.back() << " "; //last element of vector
- cout << v.at(4) << " "; //vector at index i
- v.clear(); //to clear the vector;
- //INSERT FUNCTION
- v.insert(iterator,5);//inset 5 at the iterator position iterator =v.begin();
- v.insert(it, 2, 4); //// inserts 4 two times at iterator position ;
- v.insert(position, iterator1 , iterator2) //
- //ERASE FUNCTION;
- v.erase(it); //removes the element at the iterator position ;
- v.erase(it1,it2)//removes the elements from it1 to it2(!=it2);
- v.swap(v2);//swap v1 and v2;
- //SORT
- sort(v.begin(),v.end()); //sort in ascend. order;
- sort(v.rbegin(),v.rend()); //sort in descend order;
- return 0;
- }
Add Comment
Please, Sign In to add comment