#include #include #include using namespace std; int main() { // The map data structure is an associative container // that maps one data type to another (e.g. string to int) // e.g. map "the" to 140 "the" ==> 140 // e.g. map "Achilles" to 6 "Achilles" ==> 6 // key , value map< string, int > words; map< string, int >::iterator i; // other examples: // map< int, vector > m1; // maps int keys to vectors-of-strings // map< int, Word_Node > m2; // map< string, Word_Node > m3; // e.g. in the Word_Node class, we might // // have count, vector indexes, // // snippets // map< Student, vector > m4; string w; while ( cin >> w ) { // the key is specified in [key] // words[w] = words[w] + 1; // words[s]++; // the operator[] function is important i = words.find( w ); // do the lookup, but don't add it yet // if not found in the map if ( i == words.end() ) { words.insert( make_pair( w, 1 ) ); #if 0 pair< map< string, int >::iterator, bool > p; p = words.insert( make_pair( w, 1 ) ); i = p.first; bool inserted = p.second; #endif } } // use an iterator to iterate through the elements of the map // and display them map< string, int >::const_iterator i = words.begin(); while ( i != words.end() ) { // key value cout << i->first << " (count: " << i->second << ")\n"; i++; } return 0; }