#include #include #include #include #include #include #include #include #include using namespace std; template ostream & operator<< ( ostream & out, const vector & p ) { printCollection( "", p, out ); return out; } template ostream & operator<< ( ostream & out, const pair & p ) { return out << "[ " << p.first << ", " << p.second << " ]"; } template void printCollection( const string & msg, const Collection & c, ostream & out = cout ) { Collection::const_iterator itr = c.begin( ); out << msg << ": "; while( itr != c.end( ) ) out << *itr++ << " "; out << endl; } template void printCollection( const string & msg, Iterator start, Iterator stop ) { Iterator itr = start; cout << msg << ": "; while( itr != stop ) cout << *itr++ << " "; cout << endl; } template void printCollectionInReverse( const string & msg, Collection c ) { Collection::const_reverse_iterator itr = c.rbegin( ); cout << msg << ": "; while( itr != c.rend( ) ) cout << *itr++ << " "; cout << endl; } class IsVowel { public: bool operator() ( char c ) const { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } }; class MultiplyBy10 { public: int operator() ( int x ) const { return x * 10; } }; class CaseInsensitiveLessThan { public: bool operator() ( const string & lhs, const string & rhs ) const { return stricmp( lhs.c_str( ), rhs.c_str( ) ) < 0; }; }; typedef set MY_STRING_SET; int main( ) { vector c1; c1.push_back( 3 ); c1.push_back( 4 ); c1.push_back( 7 ); list c2; c2.push_back( 3.2 ); c2.push_back( 4.1 ); c2.push_front( 1.7 ); MY_STRING_SET c3; c3.insert( "hello" ); c3.insert( "world" ); c3.insert( "happy" ); c3.insert( "zebra" ); c3.insert( "ant" ); c3.insert( "this" ); c3.insert( "ZOO" ); c3.insert( "JUNK" ); /* string s4 = "superfluous"; printCollection( "Collection c1", c1 ); printCollection( "Collection c2", c2 ); printCollection( "Collection c3", c3 ); printCollection( "String s4", s4 ); printCollectionInReverse( "rev String s4", s4 ); int arr[ ] = { 3, 5, 4, 2, 1 }; printCollection( "Collection c1", c1.begin( ), c1.end( ) ); printCollection( "Collection c2", c2.begin( ), c2.end( ) ); printCollection( "Collection c3", c3.begin( ), c3.end( ) ); printCollection( "String s4", s4.begin( ) + 2, s4.end( ) - 3 ); printCollection( "rev String s4", s4.rbegin( ), s4.rend( ) ); printCollection( "Array arr", arr, arr + 5 ); transform( arr, arr + 5, arr, MultiplyBy10( ) ); printCollection( "Array arr", arr , arr + 5 ); string::reverse_iterator itr = s4.rbegin( ); while( true ) { itr = find_if( itr, s4.rend( ), IsVowel( ) ); if( itr != s4.rend( ) ) { cout << "Found " << *itr << " at index " << endl; itr++; } else break; } */ /* printCollection( "Collection c3", c3.begin( ), c3.end( ) ); MY_STRING_SET::iterator itr1 = c3.find( "hello" ); MY_STRING_SET::iterator itr2 = c3.find( "world" ); if( itr1 == c3.end( ) || itr2 == c3.end( ) ) cout << "String in range not found" << endl; else c3.erase( itr1, ++itr2 ); printCollection( "Collection c3", c3.begin( ), c3.end( ) ); */ map phoneBook; phoneBook[ "Mitch" ] = "3053482400"; phoneBook[ "Fran" ] = "3056715678"; /* map::iterator itr = phoneBook.find( "Fran" ); if( itr == phoneBook.end( ) ) cout << "Key is not found" << endl; else cout << "Found key/value: " << itr->first << " " << itr->second << endl; */ map > index; ifstream fin( "data1.txt" ); string oneLine; string thisWord; int lineNum = 0; while( getline( fin, oneLine ) ) { lineNum++; istringstream is( oneLine ); while( is >> thisWord ) index[ thisWord ].push_back( lineNum ); } printCollection( "Count map: ", index ); return 0; }