Solved

How to use multimap?

Posted on 2004-04-13
7
819 Views
Last Modified: 2008-03-04
I have 2 input files a.txt & b.txt

contents of a.txt are
Auckland,1747564,-368536
Christchurch,1726460,-435233
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,1706
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,txt,org_cu,strt;
    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),city_n(city_n1),cu(cu1),txt(txt1),org_cu(org_cu1),strt(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,pc,loc_poco,adm,cu_poco,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_iterator 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??

0
Comment
Question by:girija_cv
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 10812016
This is how to use a multimap ...

#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("Yoko Ono", "John Lennon"));


0
 

Author Comment

by:girija_cv
ID: 10812070
can you give the full coding for above program?
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10812120
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:girija_cv
ID: 10812439
I am having only 180 points.So I am increasing points to 100.Can anyone give me the coing?

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 100 total points
ID: 10826412
0
 

Author Comment

by:girija_cv
ID: 10831568
in multimap if I use find..

 CityParms& cityParms  = cmap[city]; how to assign this?

CityParmsMap::const_iterator 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;
        }    
       
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10831649
>> 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_iterator 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

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20953562.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

       

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now