nrk009

vectorSTL

Aug 2nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.         vector<int>v(5,5);//a vector with n elements and all equal to 5;
  6.         for(int i=0 ; i < 5; i++)
  7.                v.push_back(i+1);
  8.         //PRINT VECTOR
  9.         vector<int>::iterator it;
  10.         for(it=v.begin();it!=v.end();it++)
  11.                 cout<<*it<<" ";
  12.         for(auto it:v)
  13.                 cout<<it<<" ";
  14.         for(auto it=v.begin();it!=v.end();it++)
  15.                 cout<<*it<<" ";
  16.         vector<int>::reverse_iterator itr;
  17.         for(itr=v.rbegin();itr!=v.rend();itr++)
  18.                 cout<<*itr<<" ";
  19.         for(auto itr=v.rbegin();itr!=v.rend();itr++)
  20.                 cout<<*itr<<" "*/
  21.  
  22.         //REVERSE PRINT VECTOR
  23.  
  24.          vector<int>::iterator it1=v.end();
  25.          it1--;
  26.          for(;it1>=v.begin();it1--)
  27.                 cout<<*it1<<" ";
  28.  
  29.          while(it1!=v.begin()){
  30.                 it1--;
  31.          cout<<*it1<<" ";
  32.          }
  33.  
  34.  
  35.          if(v.empty())
  36.                 cout<<"vector is empty"<<" "<<v.empty();//value of v.empty()=1;
  37.         else
  38.                 cout<<"not empty"<<v.empty()<<" ";//value of v.empty()=0;
  39.  
  40.         cout<<v.front()<<" "; //first element of vector
  41.         cout << v.back() << " "; //last element of vector
  42.         cout << v.at(4) << " "; //vector at index i
  43.  
  44.         v.clear(); //to clear the vector;
  45.  
  46.         //INSERT FUNCTION
  47.         v.insert(iterator,5);//inset 5 at the iterator position  iterator =v.begin();
  48.         v.insert(it, 2, 4); //// inserts 4 two times at iterator position ;
  49.         v.insert(position, iterator1 , iterator2) //
  50.  
  51.         //ERASE FUNCTION;
  52.         v.erase(it); //removes the element at the iterator position ;
  53.         v.erase(it1,it2)//removes the elements from it1 to it2(!=it2);
  54.  
  55.         v.swap(v2);//swap v1 and v2;
  56.  
  57.         //SORT
  58.         sort(v.begin(),v.end()); //sort in ascend. order;
  59.         sort(v.rbegin(),v.rend()); //sort in descend order;
  60.                 return 0;
  61. }
Add Comment
Please, Sign In to add comment