#include "CityGrouper.h" CityGrouper::CityGrouper(string InputFileName) { ifstream istream; string line, city, country; int pos; istream.open(InputFileName.c_str(), ios_base::in); if(!istream) { cout << "Error opening file: " << InputFileName << endl; system("pause"); exit(0); } while (!istream.eof()) { //get string getline(istream, line); //split string pos = line.find(','); city = line.substr(0, pos); country = line.substr(pos+2); //store string country2city.insert( make_pair(country, city) ); //check to see if country is already in keys set::iterator key = countries.find(country); //Didn't find the country(key) in keys if (key == countries.end()) { //add it countries.insert(country); } } istream.close(); } void CityGrouper::GroupCitiesByCountry(void) { set::iterator keyIt; for (keyIt = countries.begin(); keyIt != countries.end(); ++keyIt) { pair::iterator, hash_multimap::iterator> itPair; hash_multimap::iterator it; //get equal range of each country itPair = country2city.equal_range(*keyIt); cout << itPair.first->first << endl << endl; for(it = itPair.first; it != itPair.second; ++it) { cout << "\t" << it->second << endl; } cout << endl; } }