// List.cpp - Demonstrating the STL list class. // disable warnings when using STL container classes // with strings. Only affects DEBUG target. #ifdef _DEBUG #pragma warning( disable : 4786 ) #endif #include #include #include #include #include using namespace std; #include "item.h" #include "catalog.h" // Catalog class void ToUpperCase( string & s ) { for(int i = 0; i < s.size(); i++) s[i] = toupper( s[i] ); } void ListUpper( list & sList ) // convert each string to uppercase // uses a non-const list iterator { list::iterator pos; for(pos = sList.begin(); pos != sList.end(); pos++) { ToUpperCase(*pos); } } void ShowList( const list & sList ) // display the list // uses a const list iterator { list::const_iterator pos = sList.begin(); while( pos != sList.end()) { cout << *pos << endl; pos++; } } void ShowReverse( const list & sList ) // display list in reverse order { list::const_reverse_iterator pos = sList.rbegin(); while( pos != sList.rend()) { cout << *pos << endl; pos++; } } void Example1() // demonstrate adding and removing items { list staff; staff.push_back("Fred"); staff.push_back("Jim"); staff.push_back("Anne"); staff.push_back("Susan"); staff.pop_back(); cout << "size: " << staff.size() << endl; } void Example2() // demonstrate list iterators { list staff; staff.push_back("Fred"); if( !staff.empty()) { list::iterator pos = staff.begin(); cout << *pos << endl; // "Fred" // assign a new value *pos = "Barry"; cout << *pos << endl; // "Barry" } // erase a range of list items staff.erase( staff.begin(), staff.end() ); ShowList(staff); } void Example3() // Create and merge two lists in alphabetical order { list staff1; staff1.push_back("Anne"); staff1.push_back("Fred"); staff1.push_back("Jim"); staff1.push_back("Susan"); list staff2; staff2.push_back("Barry"); staff2.push_back("Charles"); staff2.push_back("George"); staff2.push_back("Ted"); staff2.merge( staff1 ); ListUpper( staff2); ShowList( staff2 ); } void Example4() // using the erase() function { list staff; staff.push_back("Barry"); staff.push_back("Charles"); list::iterator pos; pos = staff.begin(); staff.erase(pos); //cout << *pos; // error! pointer is invalid now // erase all elements staff.erase( staff.begin(), staff.end()); cout << staff.empty() << endl; // true } void Example5() // List of catalog items { //Create a catalog and add some items. Catalog catCurrent; catCurrent.Add( Item("00001","Chinese TaiChi Sword",75.00)); catCurrent.Add( Item("00002","Fantasy Dragon Sword",125.00)); catCurrent.Add( Item("00003","Japanese Taichi Sword",85.00)); catCurrent.Add( Item("00004","Ornate Samurai Sword",150.00)); catCurrent.Add( Item("00005","Bamboo Practice Sword",35.00)); Catalog catBackup(catCurrent); // Notice how C++ creates an automatic copy constructor catBackup = catCurrent; // C++ also creates an automatic assignment operator // Search for an item list::iterator iter = NULL; iter = catCurrent.Find( Item("00003") ); if( iter != NULL ) { cout << "Item found:\n" << *iter << endl; } // Remove the item we just found catCurrent.Remove( iter ); // Don't try to use the same iterator! (runtime error) //catCurrent.Remove( iter ); // Try to find it again -- it won't be found iter = catCurrent.Find( Item("00003") ); if( iter != NULL ) { cout << "Item found:\n" << *iter << endl; } // Display the entire catalog cout << "\n--- Catalog Contents ---\n" << catCurrent; // Save it in a file ofstream outfile("catalog.txt"); outfile << catCurrent; } int main() { // cout << boolalpha; // cout.setf(ios_base::boolalpha); cout << "List.cpp - testing the Standard C++ list class\n" "-----------------------------------------------------------\n\n"; //Example1(); //Example2(); //Example3(); //Example4(); Example5(); // list of catalog items }