------------------------------------------------------------------------------
MC logo
Vector Demo
[^] Code Examples
------------------------------------------------------------------------------
<<Template Functions vec.cc Vector Demo With Iterators>>
// 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)
{
        int limit = v.size() - 2;
        int m = 0;
        for(; m <= limit; m++)
                p << v[m] << " ";
        if(v.size()) p << v[m];

        return p;
}

main()
{
        vector<string> v;

        v.push_back("How");
        v.push_back("are");
        v.push_back("you");
        v.push_back("today");
        v[2] = "y'all";

        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);
        cout << q << endl;
}
<<Template Functions Vector Demo With Iterators>>