#include #include #include #include using namespace std; typedef map ci_map; // typedefs are often convenient when working with maps typedef multimap ci_multimap; void main() { char* first_names[] = {"Jeanette", "Lars", "Thomas", "Karsten", "Thomas", "Thomas"}; char* wishes[] = {"Trivial Pursuit", "Monopoly", "Yatzy", "Pictionary", "Jeopardy", "Monopoly"}; char* games[] = {"Trivial Pursuit", "Monopoly", "Yatzy", "Pictionary","Jeopardy"}; float prices[] = {699.95, 299.95, 59.95, 399.95, 499.95}; // declare empty maps ci_multimap christmas_wishes; ci_map board_games; // fill with contents of data arrays for (int i = 0; i < sizeof(first_names)/sizeof(char*); i++) { christmas_wishes.insert(make_pair(first_names[i], wishes[i])); } for (int j = 0; j < sizeof(games)/sizeof(char*); j++) { board_games.insert(make_pair(games[j], prices[j])); } ci_multimap::const_iterator ci_iter; cout << "Here are the family members in alphabetical order: " << endl; for (ci_iter = christmas_wishes.begin(); ci_iter != christmas_wishes.end(); ci_iter++) { cout << ci_iter->first << endl; } cout << "The Thomas'es of the family would like the following: " << endl; // equal_range returns a pair of iterators demarcating the range of elements whose key == "Thomas" pair return_value = christmas_wishes.equal_range("Thomas"); for (ci_iter = return_value.first; ci_iter != return_value.second; ci_iter++) { cout << ci_iter->second << endl; } float total = 0; // find(key) returns iterator which points to a pair whose second element is the price of the desired board game for (ci_iter = christmas_wishes.begin(); ci_iter != christmas_wishes.end(); ci_iter++) { total += (board_games.find(ci_iter->second))->second; } cout << "The total cost of making the entire family happy is: " << total << endl; }