// // Author: Robert H. Sloan // Date: Sept. 4, 2002 // // Very short routine demonstrating an STL list being used. Notice // that in the print subroutine we must use const_iterator, since the // argument is passed by constant reference. // /////////////////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; void printList( const list< string > & ); int main() { list< string > list1; // unspecified length list list< string > list2( 10 ); // 10-element list // print list1 size and contents cout << "Size of list list1 is " << list1.size() << endl; // print list2 size and contents cout << endl << "Size of vector list2 is " << list2.size() << endl; // Store stuff in list 1 list1.push_front("The"); list1.push_back("Cat"); list1.push_back("in"); list1.push_back("the"); list1.push_back("hat"); cout << endl << "After putting stuff in, list1 size is:" << endl << list1.size() << endl; // use overloaded inequality (!=) operator cout << endl << "Evaluating: list1 != list2" << endl; if ( list1 != list2 ) cout << "list1 and list2 are not equal" << endl; // Printing out list with subroutine using iterator cout << "Here is contents of list 1." << endl; printList( list1 ); return 0; } // end main void printList( const list< string > & l) { list::const_iterator itr = l.begin(); while (itr != l.end()) { cout << *itr << endl; itr++; } }