/* Filename: vector.cpp Author: Br. David Carlson Date: June 23, 1999 Revised: August 1, 2000 This program puts some values into a vector and prints them out just to illustrate some of the flexibility of the STL's vector container class. */ #include #include #include using namespace std; // Function prototype: template void Print(vector & Vec); int main(void) { const int StartSize = 8; vector Num(StartSize); vector::iterator p; int k; for (k = 0; k < StartSize; k++) Num[k] = 2 * k + 1; cout << "Contents of vector Num:" << endl; for (k = 0; k < Num.size(); k++) cout << Num[k] << " "; cout << endl << endl; cout << "We can add -55 onto the end of the vector." << endl; Num.push_back(-55); cout << "We can insert -44 before the item at index 4." << endl; p = Num.begin() + 4; Num.insert(p, -44); cout << "We can modify the item at index 0 by adding 100." << endl; Num[0] = Num[0] + 100; cout << endl; cout << "Let's use the iterator to print the vector's contents:" << endl; for (p = Num.begin(); p != Num.end(); p++) cout << *p << " "; cout << endl << endl; p = find(Num.begin(), Num.end(), 7); if (p == Num.end()) cout << "Could not find 7 in the vector" << endl; else cout << "Have found 7 in the vector" << endl; cout << "Let's sort the vector:" << endl; sort(Num.begin(), Num.end()); cout << "Let's call a function to do the printing:" << endl; Print(Num); return 0; } /* Given: Vec A vector containing items of type T. Task: To print out the contents of vector Vec. Return: Nothing. */ template void Print(vector & Vec) { vector::iterator p; cout << "Contents of vector:" << endl; for (p = Vec.begin(); p != Vec.end(); p++) cout << *p << " "; cout << endl << endl; }