The aim of this program is to create a little quiz for kids or even adults. To quickly learn the capital city for each state in the United States. Comments are added for explanation.
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
//State city class definition
class stateCity
{
public:
stateCity (const string& name = “”, const string& city = “”):
stateName(name), cityName(city)
{}
// output the state and city name in the format
// cityName, stateName
friend ostream& operator<< (ostream& ostr, const stateCity& state)
{
ostr << state.cityName << “, ” << state.stateName;
return ostr;
}
// operators < and == must be defined to use with set object.
// operators use only the stateName as the key
friend bool operator< (const stateCity& a, const stateCity& b)
{
return a.stateName < b.stateName;
}
friend bool operator== (const stateCity& a, const stateCity& b)
{
return a.stateName == b.stateName;
}
private:
string stateName, cityName;
};
int main()
{
string state;
//set declaration
cout<<endl<<“Set part of the program”<<endl<<endl;
stateCity stateCityArr[] ={stateCity(“Arizona”,”Phoenix”),
stateCity(“Illinois”,”Chicago”),stateCity(“California”,”Sacramento”)};
int stateCityArrSize = sizeof(stateCityArr)/sizeof(stateCity);
set<stateCity> s(stateCityArr,stateCityArr+stateCityArrSize);
set<stateCity> ::iterator iter;
cout<<“Please enter the name of a state:”;
getline(cin,state);
iter = s.find(state);
if(iter == s.end())
cout<<state<<” is not in the set”<<endl;
else
cout<<*iter<<endl<<endl;
//multiset declaration
cout<<endl<<“Multiset part of the Program”<<endl<<endl;
multiset<stateCity> ms;
ms.insert(stateCity(“Arizona”,”Phoenix”));
ms.insert(stateCity(“Illinois”,”Chicago”));
ms.insert(stateCity(“Illinois”,”Gary”));
ms.insert(stateCity(“Nevada”,”Reno”));
ms.insert(stateCity(“Illinois”,”Evanston”));
ms.insert(stateCity(“California”,”Sacramento”));
multiset<stateCity> ::iterator miter;
cout<<endl<<“Please enter state:”;
getline(cin,state);
pair<multiset<stateCity> :: iterator,multiset<stateCity>::iterator> p;
p = ms.equal_range(state);
miter = p.first;
if(miter == ms.end())
cout<<state<<” is not in the set”<<endl<<endl;
else
{
while(miter!= p.second)
{
if(state == *miter)
cout<<*miter<<endl;
miter++;
}
}
//map declaration
cout<<endl<<“Map part of the program”<<endl<<endl;
map<string,string> stateCapMap;
stateCapMap[“Arizona”]=”Phoenix”;
stateCapMap[“Illinois”]=”Chicago”;
stateCapMap[“California”]=”Sacramento”;
cout<<“Please enter a state to find its capital:”;
getline(cin,state);
map<string,string>:: iterator mapiter;
mapiter = stateCapMap.find(state);
if(mapiter == stateCapMap.end())
cout<<state<<” is not in the map”<<endl<<endl;
else
cout<<(*mapiter).second<<endl<<endl;
//multimap declaration
cout<<endl<<“MultiMap part of the program”<<endl<<endl;
multimap<string,string> stateCapMultMap;
typedef pair<string,string> entry;
stateCapMultMap.insert(entry(“Arizona”,”Phoenix”));
stateCapMultMap.insert(entry(“Illinois”,”Chicago”));
stateCapMultMap.insert(entry(“Illinois”,”Gary”));
stateCapMultMap.insert(entry(“Illinois”,”Evanston”));
stateCapMultMap.insert(entry(“California”,”Sacramento”));
stateCapMultMap.insert(entry(“Nevada”,”Reno”));
cout<<“Please enter a state to see its city:”;
getline(cin,state);
multimap<string,string>:: iterator multiterator;
pair<multimap<string,string>::iterator,multimap<string,string>::iterator> j;
j = stateCapMultMap.equal_range(state);
multiterator = j.first;
if(multiterator == stateCapMultMap.end())
cout<<state<<” is not in the Multimap”<<endl<<endl;
else
{
while(multiterator != j.second)
{
if(state == (*multiterator).first)
cout<<(*multiterator).second<<endl;
multiterator++;
}
}
cin.get();//retains the output screen
return 0;
}