Advertisement
Sliver1991

Vector

Aug 31st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. int main() {
  5.     vector<int> myVector; //This vector will contain int values
  6.     int number;
  7.     do {
  8.         cout << "Feed me numbers! 0 to end! ";
  9.         cin >> number;
  10.         myVector.push_back(number); //This function puts the number at the end of the vector
  11.     } while (number!=0);
  12.  
  13.     for (auto i=myVector.begin();i!=myVector.end();i++) //Loop with pointers
  14.         cout << *i << endl;
  15.  
  16.     for (int i{};i<myVector.size();i++) //Loop with index
  17.         delete myVector[i]; //In this case it's not needed, but if each object within the vector has been allocated memory, delete it.
  18.     myVector.clear(); //Clearing the content of the vector.
  19.  
  20.     return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement