# C++ Notes: STL: Example - Vector - reverse input

Vectors (from the Standard Template Library) are an expandable array. Here is the "reverse input" example (see Buffer Overflow) written with a vector.

    // vectors/vector-reverse-input.cpp - Reverses order of input.
    // Fred Swartz - 2003-09-30
    #include <iostream>
    #include <vector>                         // (1)
    using namespace std;

    int main() {
        //--- Declare a vector.
        vector<int> v;                        // (2)

        //--- Read numbers into it.
        int temp;
        while (cin >> temp) {
            v.push_back(temp);                // (3)
        }

        //--- Get elements in reverse order.
        for (int i=v.size()-1; i>=0; i--) {   // (4)
            cout << v[i] << endl;             // (5)
        }

        return 0;
    }//end main

Notes:

1. The `<vector>` header must be included.
2. Declare the vector, enclosing the element type between < and >.
3. A vector has initial size zero. Add elements to the end by calling `push_back()` with the value as a parameter. The memory associated with a vector expands as needed.
4. The current maximum size is obtained by calling `size()`.
5. Subscription is used to access an element.