Mega Code Archive

 
Categories / C++ Tutorial / Vector
 

Using pop_back to Erase the Last Element

#include <iostream> #include <vector> int main () {     using namespace std;     vector <int> v;     v.push_back (50);     v.push_back (1);     v.push_back (987);     v.push_back (1001);     cout << "The vector contains ";     cout << v.size ();     cout << " elements before calling pop_back" << endl;     // Erase one element at the end     v.pop_back ();     cout << "The vector contains ";     cout << v.size ();     cout << " elements after calling pop_back" << endl;     cout << "Enumerating items in the vector... " << endl;     unsigned int nElementIndex = 0;     while (nElementIndex < v.size ())     {         cout << "Element at position " << nElementIndex << " is: ";         cout << v [nElementIndex] << endl;         // move to the next element         ++ nElementIndex;     }     return 0; }