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

 Enter name of the stateKey is not in populationMap