Advertisement
alien_fx_fiend

Vectors [cpp}

Sep 4th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. Vectors
  2. You can think of vectors as smart arrays. They manage storage allocation for you, expanding
  3. and contracting the size of the vector as you insert or erase data. You can use vectors much like
  4. arrays, accessing elements with the [] operator. Such random access is very fast with vectors.
  5.  
  6. It’s also fast to add (or push) a new data item onto the end (the back) of the vector. When this
  7. happens, the vector’s size is automatically increased to hold the new item.
  8. Member Functions push_back(), size(), and operator[]
  9. Our first example, VECTOR, shows the most common vector operations.
  10. // vector.cpp
  11. // demonstrates push_back(), operator[], size()
  12. #include <iostream>
  13. #include <vector>
  14. using namespace std;
  15. int main()
  16. {
  17. vector<int> v; //create a vector of ints
  18. v.push_back(10); //put values at end of array
  19. v.push_back(11);
  20. v.push_back(12);
  21. v.push_back(13);
  22. v[0] = 20; //replace with new values
  23. v[3] = 23;
  24. for(int j=0; j<v.size(); j++) //display vector contents
  25. cout << v[j] << ‘ ‘; //20 11 12 23
  26. cout << endl;
  27. return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement