------------------------------------------------------------------------------
MC logo
Vector Demo With Iterators
[^] Code Examples
------------------------------------------------------------------------------
<<Vector Demo vec2.cc Template Generalized Stack>>
// There is a standard vector class in C++, but it is based on a
// template, not on polymophism.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

// Generic printing function.
template <class T>
ostream &operator<<(ostream & p, vector<T> &v)
{
        vector<T>::iterator limit = v.end() - 2;
        vector<T>::iterator m = v.begin();
        for(; m <= limit; m++)
                p << *m << " ";
        if(v.size()) p << *m;

        return p;
}

main()
{
        vector<string> v;

        v.push_back("How");
        v.push_back("are");
        v.push_back("you");
        v.push_back("today");
        v.insert(v.begin() + 3, "doing");

        cout << v << endl;
        cout << v.front() << " " << v.back() << endl;

        vector<int> q;
        q.push_back(45);
        q.push_back(3);
        q.push_back(-34);
        q.push_back(75);
        q.push_back(9);
        vector<int>::iterator qi = q.end() - 3;
        q.insert(qi, 44);

        cout << q << endl;
}
<<Vector Demo Template Generalized Stack>>