// File: test_cs2list.cpp // Author: Chuck Stewart // Purpose: Test some of the member functions of our own // version of a list-like generic container class. #include #include #include using namespace std; #include "cs2list.h" // Adding operator== and operator!= to aid in testing.... // template bool operator==( cs2list& lft, cs2list& rgt ) { if ( lft.size() != rgt.size() ) return false; // The use of "typename" here looks a bit confusing. It eliminates // a warning issued by advanced compilers. It is not clear to the // compiler that cs2list::iterator is a real type because T // hasn't yet be specified. // // If you didn't get that, don't worry about it. You will not be // tested on it! typename cs2list::iterator lft_itr = lft.begin(); typename cs2list::iterator rgt_itr = rgt.begin(); while ( lft_itr != lft.end() ) { if ( *lft_itr != *rgt_itr ) return false; lft_itr ++ ; rgt_itr ++ ; } return true; } template bool operator!=( cs2list& lft, cs2list& rgt ) { return ! ( lft == rgt ); } int main() { const int n1=20; int i; cs2list list1; for ( i=0; i::iterator itr; for ( i=0; i<10; ++i ) list1.pop_front(); cout << "\nAfter popping 10 items off the front:\n" << "list1.size() = " << list1.size() << " should be 10\n"; for ( itr = list1.begin(); itr != list1.end(); ++itr ) cout << *itr << "\n"; cs2list int_list; int_list.push_back( 24 ); int_list.push_back( 33 ); int_list.push_back( 35 ); int_list.push_back( 15 ); int_list.push_back( 66 ); int_list.push_back( 48 ); int_list.push_back( 32 ); int_list.push_back( 3 ); int_list.push_back( 21 ); cout << "\nHere's an int list: " << endl; cs2list::iterator p; for ( p = int_list.begin(); p != int_list.end(); ++p ) cout << *p << endl; p = int_list.begin(); while ( p != int_list.end() ) if ( *p % 2 == 1 ) p = int_list.erase( p ); else p ++ ; cout << "\nHere's the int list after erasing odds:" << endl; for ( p = int_list.begin(); p != int_list.end(); ++p ) cout << *p << endl; /* This should help you test Checkpoint 2 cs2list b; b.push_back( string("hi") ); cs2list::iterator s_itr = b.begin(); s_itr = b.insert( s_itr, string("low") ); s_itr = b.insert( s_itr, string("lower") ); b.push_back( string("higher") ); s_itr ++ ; // should be at low s_itr ++ ; // should be at hi s_itr ++ ; // should be at higher s_itr = b.insert( s_itr, string("high") ); cout << "\n\nlist of strings should be: lower, low, hi, high, higher\nIt is: " << endl; for ( s_itr = b.begin(); s_itr != b.end(); ++ s_itr ) cout << *s_itr << " "; cout << endl; */ return 0; }