#include #include #include std::map dictionary; void insert_and_print(std::string word, std::string definition) try { std::pair< std::map::iterator, bool > retval; retval = dictionary.insert(std::make_pair(word,definition)); if (retval.second) std::cout << "INSERTED: " << retval.first->first << std::endl; else std::cout << "FOUND EXISTING MAP KEY: " << retval.first->first << std::endl; } catch(std::bad_alloc e) { std::cerr << "function died with bad memory allocation\n"; } void find_it(std::string key) { std::map::iterator end = dictionary.end(); std::map::iterator found = dictionary.find(key); if ( found == end ) std::cout << "KEY NOT FOUND IN MAP: " << key << std::endl; else std::cout << "WORD: " << found->first << "\tDEFINITION: " << found->second << std::endl; } int main(int argc, char** argv) { insert_and_print("coffee", "caffenated drink made from beans"); insert_and_print("bagel", "cicular bread with a whole in the middle"); insert_and_print("history", "information about stuff that happened in the past"); insert_and_print("coffee", "starbucks makes lots of money withit"); insert_and_print("bagel", "try some cream cheese with it"); insert_and_print("history", "a subject in school"); find_it("coffee"); find_it("excellence"); find_it("bagel"); std::cout << "\n\n-------------------------dictionary dump\n" << std::endl; std::map::iterator end = dictionary.end(); for ( std::map::iterator iter = dictionary.begin(); iter != end ; iter++) { std::cout << "KEY: " << iter->first << "\tVALUE: " << iter->second << std::endl; } return 0; }