girija_cv
asked on
How to use multimap?
I have 2 input files a.txt & b.txt
contents of a.txt are
Auckland,1747564,-368536
Christchurch,1726460,-4352 33
fields are city,x,y
contents of b.txt are
1706,Auckland,Williams Avenue
1706,Auckland,Willow Way
1706,Auckland,Wyman Place
fields are poco,city,str
If city of a.txt is matching with city of b.txt,I want to write the o/p in a file as follows
Auckland,Williams Avenue,1747564,-368536,170 6
Auckland,Willow Way,1747564,-368536,1706
Auckland,Wyman Place,1747564,-368536,1706
the coding is as follows
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct CityParms {
string x,y,org_n1,cu,pc,city_n,tx t,org_cu,s trt;
CityParms(const string& x1 = "",const string &y1 = "",const string org_n2 = "",const string pc1 = "",
const string city_n1 = "", const string cu1 = "",const string txt1 = "",const string org_cu1 = "",const string strt1 = "")
: x(x1),y(y1),org_n1(org_n2) ,pc(pc1),c ity_n(city _n1),cu(cu 1),txt(txt 1),org_cu( org_cu1),s trt(strt1) {}
};
typedef map<string, CityParms> CityParmsMap;
typedef vector<CityParms*> CityParmsArr;
void process_10_14(CityParmsMap & cmap)
{
string fr_str,org_n1,loc,cu,x,y,p c,loc_poco ,adm,cu_po co,org_cu;
ifstream fr("a.txt");
if (!fr.is_open())
{
cout << "Unable to open file: " << "a.txt" << ": Aborting" << endl;
exit(1);
}
while(!fr.eof())
{
getline(fr,fr_str);
std::istringstream istr(fr_str);
getline(istr,loc,',');
getline(istr,x,',');
getline(istr,y,',');
//cout << a_adm << "#" << city << "#" << poco << endl;
org_n1 = loc;
CityParms& cityParms = cmap[loc];
cityParms.city_n = org_n1;
cityParms.x = x;
cityParms.y = y;
}
}
void process_a_str(CityParmsMap & cmap, CityParmsArr& carr)
{
std::ifstream f("b.txt");
if (!f.is_open())
{
cout << "Unable to open file: " << ": Aborting" << endl;
exit(1);
}
string str;
while (!f.eof())
{
getline(f,str);
if (str.empty())
break;
std::istringstream istr(str);
string city, poco, strt;
getline(istr,poco,',');
if (poco.empty())
break;
getline(istr,city,',');
if (city.empty())
break;
getline(istr,strt,',');
if (strt.empty())
break;
//cout << a_adm << "#" << city << "#" << poco << endl;
cout << "Processing address " << city << endl;
CityParmsMap::const_iterat or Citr = cmap.find(city);
cout << "processing 10-14 cities " <<city << endl;
if ( Citr != cmap.end())
{
CityParms& cityParms = cmap[city];
CityParms* pCityParms = new CityParms(cityParms.x, cityParms.y, cityParms.org_n1, poco,
city, cityParms.cu, cityParms.txt, cityParms.org_cu, strt);
carr.push_back(pCityParms) ;
cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched" << endl;
}
}
}
void str_out(const CityParmsArr& carr,const string& s_out)
{
std::ofstream fo(s_out.c_str());
string bl;
if (!fo.is_open())
{
cout << "Unable to open file: " << s_out.c_str() << ": Aborting" << endl;
exit(1);
}
for (int i = 0; i < carr.size(); ++i)
{
CityParms* pCityParms = carr[i];
CityParms& cityParms = *pCityParms;
fo << cityParms.city_n<< ',';
fo << cityParms.strt << ',';
fo << cityParms.x << ',';
fo << cityParms.y << ',';
fo << cityParms.pc;
fo << '\n';
}
}
void main()
{
CityParmsMap cmap;
CityParmsArr carr;
process_10_14(cmap);
process_a_str(cmap, carr);
str_out(carr, "c.txt");
}
I want to do this program using multimap.Can anyone give me the coding??
contents of a.txt are
Auckland,1747564,-368536
Christchurch,1726460,-4352
fields are city,x,y
contents of b.txt are
1706,Auckland,Williams Avenue
1706,Auckland,Willow Way
1706,Auckland,Wyman Place
fields are poco,city,str
If city of a.txt is matching with city of b.txt,I want to write the o/p in a file as follows
Auckland,Williams Avenue,1747564,-368536,170
Auckland,Willow Way,1747564,-368536,1706
Auckland,Wyman Place,1747564,-368536,1706
the coding is as follows
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct CityParms {
string x,y,org_n1,cu,pc,city_n,tx
CityParms(const string& x1 = "",const string &y1 = "",const string org_n2 = "",const string pc1 = "",
const string city_n1 = "", const string cu1 = "",const string txt1 = "",const string org_cu1 = "",const string strt1 = "")
: x(x1),y(y1),org_n1(org_n2)
};
typedef map<string, CityParms> CityParmsMap;
typedef vector<CityParms*> CityParmsArr;
void process_10_14(CityParmsMap
{
string fr_str,org_n1,loc,cu,x,y,p
ifstream fr("a.txt");
if (!fr.is_open())
{
cout << "Unable to open file: " << "a.txt" << ": Aborting" << endl;
exit(1);
}
while(!fr.eof())
{
getline(fr,fr_str);
std::istringstream istr(fr_str);
getline(istr,loc,',');
getline(istr,x,',');
getline(istr,y,',');
//cout << a_adm << "#" << city << "#" << poco << endl;
org_n1 = loc;
CityParms& cityParms = cmap[loc];
cityParms.city_n = org_n1;
cityParms.x = x;
cityParms.y = y;
}
}
void process_a_str(CityParmsMap
{
std::ifstream f("b.txt");
if (!f.is_open())
{
cout << "Unable to open file: " << ": Aborting" << endl;
exit(1);
}
string str;
while (!f.eof())
{
getline(f,str);
if (str.empty())
break;
std::istringstream istr(str);
string city, poco, strt;
getline(istr,poco,',');
if (poco.empty())
break;
getline(istr,city,',');
if (city.empty())
break;
getline(istr,strt,',');
if (strt.empty())
break;
//cout << a_adm << "#" << city << "#" << poco << endl;
cout << "Processing address " << city << endl;
CityParmsMap::const_iterat
cout << "processing 10-14 cities " <<city << endl;
if ( Citr != cmap.end())
{
CityParms& cityParms = cmap[city];
CityParms* pCityParms = new CityParms(cityParms.x, cityParms.y, cityParms.org_n1, poco,
city, cityParms.cu, cityParms.txt, cityParms.org_cu, strt);
carr.push_back(pCityParms)
cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched" << endl;
}
}
}
void str_out(const CityParmsArr& carr,const string& s_out)
{
std::ofstream fo(s_out.c_str());
string bl;
if (!fo.is_open())
{
cout << "Unable to open file: " << s_out.c_str() << ": Aborting" << endl;
exit(1);
}
for (int i = 0; i < carr.size(); ++i)
{
CityParms* pCityParms = carr[i];
CityParms& cityParms = *pCityParms;
fo << cityParms.city_n<< ',';
fo << cityParms.strt << ',';
fo << cityParms.x << ',';
fo << cityParms.y << ',';
fo << cityParms.pc;
fo << '\n';
}
}
void main()
{
CityParmsMap cmap;
CityParmsArr carr;
process_10_14(cmap);
process_a_str(cmap, carr);
str_out(carr, "c.txt");
}
I want to do this program using multimap.Can anyone give me the coding??
ASKER
can you give the full coding for above program?
The policy in EE is not to do the programs for you, but to help you do it yourself and help you understand what you are doing.
Also for 20 points nobody is going to bother ... even not those who do not follow EE guidelines and policies.
Try to increase the point to 250 and pray.
Nevertheless, I and other EE experts will be more than happy to assist you in coding it yourself.
Try to do it and post here questions to the problems you encounter
Moreover, you have to decide how to treat multiple values ... we can only guess.
Good luck,
Mafalda.
Also for 20 points nobody is going to bother ... even not those who do not follow EE guidelines and policies.
Try to increase the point to 250 and pray.
Nevertheless, I and other EE experts will be more than happy to assist you in coding it yourself.
Try to do it and post here questions to the problems you encounter
Moreover, you have to decide how to treat multiple values ... we can only guess.
Good luck,
Mafalda.
ASKER
I am having only 180 points.So I am increasing points to 100.Can anyone give me the coing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
in multimap if I use find..
CityParms& cityParms = cmap[city]; how to assign this?
CityParmsMap::const_iterat or Citr = cmap.find(city);
cout << "processing 10-14 cities " <<city << endl;
if ( Citr != cmap.end())
{
CityParms& cityParms = cmap[city];
CityParms* pCityParms = new CityParms(cityParms.x, cityParms.y, cityParms.org_n1, poco,
city, cityParms.cu, cityParms.txt, cityParms.org_cu, strt);
carr.push_back(pCityParms) ;
cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched" << endl;
}
CityParms& cityParms = cmap[city]; how to assign this?
CityParmsMap::const_iterat
cout << "processing 10-14 cities " <<city << endl;
if ( Citr != cmap.end())
{
CityParms& cityParms = cmap[city];
CityParms* pCityParms = new CityParms(cityParms.x, cityParms.y, cityParms.org_n1, poco,
city, cityParms.cu, cityParms.txt, cityParms.org_cu, strt);
carr.push_back(pCityParms)
cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched" << endl;
}
>> CityParms& cityParms = cmap[city]; how to assign this?
As multimap may have more than one entry to a given key, there is no operator[] defined.
However, you may do this:
CityParmsMap::iterator Citr = cmap.find(city);
if (Citr != cmap.end())
{
CityParms& cp = Citr->second;
....
}
You see, multimap is much less comfortable than map.
>> CityParmsMap::const_iterat or Citr = cmap.find(city);
Also because of multiple keys you may not use a 'const_iterator' but an 'iterator' whrere you could iterate thru all entries having same key.
Look at last implementation
https://www.experts-exchange.com/questions/20953562/How-to-match-duplicate-cities-in-map.html
to learn that multimap functionality hadn't actually used. We could have made the same using a vector<CityParms> or a list<CityParms> or even a normal map by using a different key - maybe x + y.
Regards, Alex
As multimap may have more than one entry to a given key, there is no operator[] defined.
However, you may do this:
CityParmsMap::iterator Citr = cmap.find(city);
if (Citr != cmap.end())
{
CityParms& cp = Citr->second;
....
}
You see, multimap is much less comfortable than map.
>> CityParmsMap::const_iterat
Also because of multiple keys you may not use a 'const_iterator' but an 'iterator' whrere you could iterate thru all entries having same key.
Look at last implementation
https://www.experts-exchange.com/questions/20953562/How-to-match-duplicate-cities-in-map.html
to learn that multimap functionality hadn't actually used. We could have made the same using a vector<CityParms> or a list<CityParms> or even a normal map by using a different key - maybe x + y.
Regards, Alex
#include <map>
using std::map;
using std::multimap;
// Using a map
map<string, string> my_map;
my_map["David"] = "Goliath";
my_map["Yoko Ono"] = "John Lennon";
//Using a multimap is a little more complicated
multimap<string, string> my_mm;
typedef std::multimap<string, string>::value_type val_type;
my_mm.insert(val_type("Yok