Solved

How to use multimap?

Posted on 2004-04-13
7
822 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with listview limit text 20 104
Grammars for C C++ and java 1 122
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 138
Precision Problem in C++ 7 33
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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