[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use multimap?

Posted on 2004-04-13
7
Medium Priority
?
843 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

873 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