#include #include void print ( vector T ) { cout << endl; // using an interator provides checked access to the vector // think of the iterator as a pointer to an element of the vector. // In this case, the iterator i points to string*, therefore it is // dereferenced twice vector::const_iterator i; for (i=T.begin() ; i < T.end() ; ++i) cout << **i << endl; cout << endl; // you can also access elements this simpler way. Note that the size() // method returns the number of elements in the vector. T[j] is an // element of the vector, that is, of type string*, thus we dereference once. for (int j=0 ; j < T.size() ; ++j) cout << *T[j] << endl; cout << endl; } void main() { vector list; // note that list.insert( "some string" ) won't work because // list as defined will only accept pointers or references to strings // not the strings themselves. list.insert( 0, new string("I love C++") ); // first arg is position in list list.push_back( new string("I love algorithms") ); list.push_back( new string("I love data structures") ); print( list ); // print the list twice in regular order // remove the elements as you would from a stack and "process" // them by printing them out. // important: the vector method pop_back() removes the back element // but does not return anything. Before using pop_back() you must get the // back element first: while ( ! list.empty() ) { cout << *( list.back() ) << endl; list.pop_back(); } // to see that the vector is now empty: cout << endl << "the size is now: " << list.size() << endl; }