/* Final Map Associative Container Program */
#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;
int main()
{
typedef map<string, int>mapType;
mapType populationMap;
int i;
int num;
string sname;
int popnum;
cout<<"How many states you want to enter in a Map"<<endl;
cin>>num;
for(i=0;i<num;i++)
{
cout<<"Enter State name"<<endl;
cin>>sname;
cout<<"Enter Population"<<endl;
cin>>popnum;
cout<<"***********************************************************************************"<<endl;
populationMap.insert(pair<string, int>(sname, popnum));
}
mapType::iterator iter = --populationMap.end();
// output the size of the map
cout<< "Size of populationMap: " <<populationMap.size() << '\n';
for (iter = populationMap.begin(); iter != populationMap.end(); ++iter) {
cout<<iter->first <<": " <<iter->second << " million\n";
cout<<"***********************************************************************************"<<endl;
}
// find will return an iterator to the matching element if it is found
// or to the end of the map if the key is not found
string state;
cout<<"Enter the name of the state to be find in Map"<<endl;
cin>>state;
//string country("Indonesia");
iter = populationMap.find(state);
if( iter != populationMap.end() )
cout<< state <<"'s populations is "
<<iter->second << " million\n";
else
cout<< "Key is not in populationMap" << '\n';
// clear the entries in the map
populationMap.clear();
}
Sunday, January 10, 2021
Final Map Associative Container Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment