Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Vector
- //===============
- // Declaration
- //=============
- vector <int> v; // Vector of Size Zero of 'int' type
- vector <double> v; // Vector of Size Zero of 'double' type
- vector <int> v(N); // Vector of Size 'N' initialized with all '0'
- vector <int> v(N,x); // Vector of Size 'N' initialized with all 'x'
- // Some References
- //=================
- v.begin(); // Address of the first element
- v.end(); // Address *after the last element, not the last element
- v.data(); // Address of the first element, C-style pointer
- // Access Data
- //=============
- v.front(); // First Element
- v.back(); // Last Element
- //'i'th Index
- v[i]; // Direct Method
- v.at(i); // STD Style
- int* p = v.data(); // Return the pointer like arrays in C
- p[i]; // C-Style (Efficient)
- // Input Data
- //============
- v.push_back(x); // Insert item 'x' at the end
- v.insert(v.begin(), x); // Insert 'x' at the beginning
- v.insert(v.begin()+i, x); // Insert 'x' at the 'i'th index
- v.insert(v.begin()+i, n, x);// Insert 'n' copies of 'x' at the 'i'th index
- // Delete Data
- //=============
- v.pop_back(); // Deletes the last item
- v.erase(v.begin()); // Deletes the first element
- v.erase(v.begin()+i); // Deletes the 'i'th indexed element
- v.erase(v.begin()+i, v.begin()+j); // Deletes from index 'i' to 'j-1'
- v.erase(v.begin()+i, v.end); // Deletes from index 'i' to end
- v.clear(); // Clears the whole Vector
- // Miscellaneous Functions
- //=========================
- v.size(); // return the size of the vector
- v.empty(); // return 'true' if empty, returns 'false' if non-empty
- sort(v.begin(), v.end()); //sorts the vector in ascending order
- reverse(v.begin(), v.end()); //reverse the vector
- swap(v, w); // Swaps the vectors 'v' and 'w'
- // Iterations
- //============
- // Declaration of Iterators
- vector <int>::iterator it; // 'int' type iterator
- // Printing All elements using Iterators
- for(vector <int>::iterator it=v.begin(); it!=v.end(); it++){
- cout << *it << " ";
- }
- // Using 'auto' keyword
- for(auto it=v.begin(); it!=v.end(); it++){
- cout << *it << " ";
- }
- // Loop Iterator (Shortcut Method)
- for(auto it:v){
- cout << it << " "; // Note: here 'it' is a value, not a pointer.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement