#include #include // for setw #include #include #include #include // for class map using namespace std; int main() { // Map city names (string) to their population (int) const map m { { "New York", 8478072 }, { "Yonkers", 211569 }, { "Hastings", 9289 }, { "Dobbs Ferry", 11541 }, { "Irvington", 6653 }, { "Tarrytown", 11860 } }; cout << "The map contains " << m.size() << " pairs:\n\n"; // Output all pairs using a range-based for loop for (const auto& pair : m) { cout << setw(12) << left << pair.first << " = " << pair.second << "\n"; } for (;;) { cout << "\nPlease type a city (or control-d to quit): "; string city; // Using getline because some city names (like New York) have spaces if (!getline(cin, city)) { break; // The user typed control-d } if (city.empty()) continue; // Use the map's find method to look up the city const auto it {m.find(city)}; if (it == end(m)) { cerr << "Could not find " << city << "\n"; } else { // it->first is the city name, it->second is the population cout << "The population of " << it->first << " is " << it->second << ".\n"; } } return EXIT_SUCCESS; }