fork download
  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. #include<utility>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. typedef map<string, int> mapType;
  10. mapType populationMap;
  11.  
  12. populationMap.insert(pair<string, int>("Maharashtra", 7026357));
  13. populationMap.insert(pair<string, int>("Rajasthan", 6578936));
  14. populationMap.insert(pair<string, int>("Karnataka", 6678993));
  15. populationMap.insert(pair<string, int>("Punjab", 5789032));
  16. populationMap.insert(pair<string, int>("West Bengal", 6676291));
  17.  
  18. mapType::iterator iter;
  19. cout << "====Population of states in India====";
  20. cout << "\nSize of populationMap: " << populationMap.size() << "\n";
  21.  
  22. string state_name;
  23. cout << "\nEnter name of the state: ";
  24. cin >> state_name;
  25. iter = populationMap.find(state_name);
  26.  
  27. if (iter != populationMap.end()) {
  28. cout << state_name << "'s population is " << iter->second << "\n";
  29. } else {
  30. cout << "Key is not in populationMap\n";
  31. }
  32.  
  33. populationMap.clear();
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
====Population of states in India====
Size of populationMap: 5

Enter name of the state: Key is not in populationMap