Mega Code Archive

 
Categories / C++ Tutorial / Vector
 

Get the last element in a vector

#include <iostream> #include <vector> using namespace std; typedef vector<int> INTVECTOR; const int ARRAY_SIZE = 4; int main(void) {    INTVECTOR theVector;    // Intialize the array to contain the members [100, 200, 300, 400]    for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)       theVector.push_back((cEachItem + 1) * 100);    cout << "Last element: " << theVector.back() << endl; }