Advertisement
parthosutradhor

Vector

Jul 5th, 2023 (edited)
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | Source Code | 0 0
  1.                                 //Vector
  2.                             //===============
  3.                            
  4.                            
  5. // Declaration
  6. //=============
  7.     vector <int> v;         // Vector of Size Zero of 'int' type
  8.     vector <double> v;      // Vector of Size Zero of 'double' type
  9.     vector <int> v(N);      // Vector of Size 'N' initialized with all '0'
  10.     vector <int> v(N,x);    // Vector of Size 'N' initialized with all 'x'
  11.    
  12.  
  13. // Some References
  14. //=================
  15.     v.begin();  // Address of the first element  
  16.     v.end();    // Address *after the last element, not the last element
  17.    
  18.     v.data();   // Address of the first element, C-style pointer
  19.  
  20.  
  21. // Access Data
  22. //=============
  23.     v.front();      // First Element
  24.     v.back();       // Last Element
  25.  
  26.     //'i'th Index
  27.     v[i];           // Direct Method
  28.     v.at(i);        // STD Style
  29.    
  30.     int* p = v.data();  // Return the pointer like arrays in C
  31.     p[i];               // C-Style (Efficient)
  32.    
  33.  
  34. // Input Data
  35. //============
  36.     v.push_back(x);             // Insert item 'x' at the end
  37.     v.insert(v.begin(), x);     // Insert 'x' at the beginning
  38.     v.insert(v.begin()+i, x);   // Insert 'x' at the 'i'th index
  39.     v.insert(v.begin()+i, n, x);// Insert 'n' copies of 'x' at the 'i'th index
  40.  
  41.  
  42. // Delete Data
  43. //=============
  44.     v.pop_back();                       // Deletes the last item
  45.     v.erase(v.begin());                 // Deletes the first element
  46.     v.erase(v.begin()+i);               // Deletes the 'i'th indexed element
  47.     v.erase(v.begin()+i, v.begin()+j);  // Deletes from index 'i' to 'j-1'
  48.     v.erase(v.begin()+i, v.end);        // Deletes from index 'i' to end
  49.     v.clear();                          // Clears the whole Vector
  50.  
  51.  
  52. // Miscellaneous Functions
  53. //=========================
  54.     v.size();   // return the size of the vector
  55.     v.empty();  // return 'true' if empty, returns 'false' if non-empty
  56.    
  57.     sort(v.begin(), v.end());       //sorts the vector in ascending order
  58.     reverse(v.begin(), v.end());    //reverse the vector
  59.    
  60.     swap(v, w);     // Swaps the vectors 'v' and 'w'
  61.    
  62.    
  63. // Iterations
  64. //============
  65.    
  66.     // Declaration of Iterators
  67.     vector <int>::iterator it;  // 'int' type iterator
  68.    
  69.     // Printing All elements using Iterators
  70.     for(vector <int>::iterator it=v.begin(); it!=v.end(); it++){
  71.         cout << *it << " ";
  72.     }
  73.    
  74.     // Using 'auto' keyword
  75.     for(auto it=v.begin(); it!=v.end(); it++){
  76.         cout << *it << " ";
  77.     }
  78.  
  79.     // Loop Iterator (Shortcut Method)
  80.     for(auto it:v){
  81.         cout << it << " "; // Note: here 'it' is a value, not a pointer.
  82.     }
  83.    
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement