-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.cpp
44 lines (35 loc) · 1.42 KB
/
map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Write a program in C++ to use map associative container. The keys will be the names of states and the values will be the populations of the states. When the program runs, the user is prompted to type the name of a state. The program then looks in the map, using the state name as an index and returns the population of the state.
*/
#include <iostream>
#include <map>
#include <string>
int main() {
// Create a map to store state names and their populations
std::map<std::string, int> statePopulation;
// Initialize the map with some state populations
statePopulation["California"] = 39538223;
statePopulation["Texas"] = 29145505;
statePopulation["Florida"] = 21538187;
statePopulation["New York"] = 20201249;
statePopulation["Pennsylvania"] = 13002700;
std::string stateName;
std::cout << "Enter the name of a state: ";
std::getline(std::cin, stateName);
// Look for the state in the map and display the population
auto it = statePopulation.find(stateName);
if (it != statePopulation.end()) {
std::cout << "The population of " << stateName << " is " << it->second << "." << std::endl;
} else {
std::cout << "State not found." << std::endl;
}
return 0;
}
/*
Enter the name of a state: Texas
The population of Texas is 29145505.
Enter the name of a state: Florida
The population of Florida is 21538187.
Enter the name of a state: Ohio
State not found.
*/