Solved

How to match duplicate cities in map?

Posted on 2004-04-14
11
339 Views
Last Modified: 2010-04-01
I have 2 input files a.txt & b.txt

contents of a.txt are
Auckland,1747564,-368536
Auckland,100000,-3556677
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
Auckland,Williams Avenue,100000,-3556677,1706
Auckland,Willow Way,100000,-3556677,1706
Auckland,Wyman Place,100000,-3556677,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");
}

this gives the output as follows
Auckland,Williams Avenue,100000,-3556677,1706
Auckland,Willow Way,100000,-3556677,1706
Auckland,Wyman Place,100000,-3556677,1706


How to get output as follows
Auckland,Williams Avenue,1747564,-368536,1706
Auckland,Willow Way,1747564,-368536,1706
Auckland,Wyman Place,1747564,-368536,1706
Auckland,Williams Avenue,100000,-3556677,1706
Auckland,Willow Way,100000,-3556677,1706
Auckland,Wyman Place,100000,-3556677,1706


Can anyone help me?
0
Comment
Question by:girija_cv
  • 4
  • 2
  • 2
11 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10822518
Hi girija_cv,
You have two choices:

Either use a multimap for several entries per key, or you use a vector for each key to hold multiple values.

If you still get duplicates, use a map hierarchy or composite keys.

Cheers,
Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 38 total points
ID: 10822575
girija_cv,
> Auckland,1747564,-368536
> Auckland,100000,-3556677
> Christchurch,1726460,-435233

Store your a.txt information in a

map<string,vector<pair<int,int> > >

Like

map<city,vector<pair<x,y> > >

And then go through b.txt, like:
foreach line of b.txt
    extract city
    foreach entry in city's vector
        display city,street,pair-x,pair-y,poco




Stefan
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10825189
Try this:

#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) {}
    CityParms(const CityParms& cp)
        : x(cp.x),y(cp.y),org_n1(cp.org_n1),pc(cp.pc),city_n(cp.city_n),cu(cp.cu),txt(cp.txt),org_cu(cp.org_cu),strt(cp.strt) {}
};

//typedef map<string, CityParms> CityParmsMap;
typedef multimap<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(x, y, loc, "", "", loc);
        cityParms.city_n = org_n1;
        cityParms.x = x;
        cityParms.y = y;
        cmap.insert(pair<string, CityParms>(loc, cityParms));
    }
}
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::iterator Citr;
        for (Citr = cmap.begin(); Citr != cmap.end(); ++Citr)
        {
            CityParms& cityParms  = Citr->second;
            cout << "processing 10-14 cities " <<cityParms.city_n << endl;

            if (cityParms.city_n == 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 replaced map by multimap as city isn't a unique key anymore. The output file is 'sorted'  city, street and not city,pair-x as in your example. If this is a problem, we had to sort the array before output.

Regards, Alex

BTW, this answers your multimap question as well ;-)

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:girija_cv
ID: 10831751
how to do it using map.find? instead of

if (cityParms.city_n == city)

           
please sugeest me..
 how to code the following statements in multimap..

   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
 

Author Comment

by:girija_cv
ID: 10839137
what does this statement mean?

CityParms cityParms(x, y, loc, "", "", loc);
------------------------------------------------------------------------------------------
Using map.find I have coded as follows.But in the output I am not getting xvalue. How to get that?Please give me modified coding..

#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,org_city,n2,cc,ty;
    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 = "",const string org_city = "",const string n2 = "",const string cc = "",const string ty = "")
        : x(x1),y(y1),org_n1(org_n2),pc(pc1),city_n(city_n1),cu(cu1),txt(txt1),org_cu(org_cu1),strt(strt1),org_city(org_city),n2(n2),cc(cc),ty(ty) {}
   // CityParms(const CityParms& cp)
    //    : x(cp.x),y(cp.y),org_n1(cp.org_n1),pc(cp.pc),city_n(cp.city_n),cu(cp.cu),txt(cp.txt),org_cu(cp.org_cu),strt(cp.strt) {}
};

//typedef map<string, CityParms> CityParmsMap;
typedef multimap<std::string, CityParms> CityParmsMap;
typedef vector<CityParms*> CityParmsArr;
static char clean(char c);

void process_10_14(CityParmsMap& cmap)
{
   
    string fr_str,org_n1,loc,cu,x,y,pc,loc_poco,adm,cu_poco,org_cu;
      int i;
   
    ifstream fr("nz_add.dat");
    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,pc,',');
            getline(istr,loc,',');
                   
        org_n1 = loc;
       
       
            for(i=0;i<=loc.length();i++)
                  {
                        loc[i] = tolower(loc[i]) ;
                        if (loc[i] == '-')
                        {
                              loc[i] = ' ';
                        }
                        if (loc[i] == '\'')
                        {
                              loc[i] = ' ';
                        }
                  }
        loc_poco = loc + pc;
                  cout << loc_poco << endl;
                  CityParms cityParms;
                  cityParms.city_n = loc_poco;
                  cityParms.org_n1 = org_n1;
                  cityParms.pc = pc;
        cmap.insert(pair<string, CityParms>(loc_poco, cityParms));
            cout <<"address " << loc << pc <<endl;
    }
}
void process_a_str(CityParmsMap& cmap, CityParmsArr& carr)
{    
   
    std::ifstream f("nz_grd.dat");
      std::ofstream fod("out");
    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,cu,x,y,n2,cc,ty,city_poco,cu_poco,txt,org_city,org_cu;
            int j;
            getline(istr,city,',');
            getline(istr,cu,',');
            getline(istr,x,',');
            getline(istr,y,',');
            getline(istr,poco,',');
            getline(istr,n2,',');
            getline(istr,cc,',');
            getline(istr,ty,',');
            org_city = city;
            org_cu = cu;
                  for (j=0;j<=city.length();j++)
                  {
                        
                        city[j] = clean(city[j]);
                city[j] = tolower(city[j]);
                  }
                  for (j=0;j<=cu.length();j++)
                  {
                        
                        cu[j] = clean(cu[j]);
                cu[j] = tolower(cu[j]);
                  }
                  city_poco = city + poco;
                  cu_poco = cu + poco;
   
                  cout << "Processing address " << city_poco << '#' <<cu_poco << endl;
       
        CityParmsMap::iterator Citr;
        for (Citr = cmap.begin(); Citr != cmap.end(); ++Citr)
        {
            CityParms& cityParms  = Citr->second;
                  cout << "processing 10-14 cities " <<cityParms.city_n<< '#' << city_poco << endl;

            if (cityParms.city_n == city_poco)
            {  
                        txt = "Matched with loc";
                CityParms* pCityParms = new CityParms(org_city,org_cu,x,y,n2,cc,ty,txt,cityParms.org_n1);
                   
                carr.push_back(pCityParms);
                cout << pCityParms->org_n1 << cityParms.pc << "matched" << endl;
            }  
                  else
                  {
                        if (cityParms.cu == cu_poco)
                        {  
                              txt = "Matched with cu";
                CityParms* pCityParms = new CityParms(org_city,org_cu,x,y,n2,cc,ty,txt,cityParms.org_n1);
                   
                carr.push_back(pCityParms);
                //cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched with cu" << endl;
                        cout << pCityParms->org_n1 << cityParms.pc << "matched" << endl;
                        }  
                        else
                        {
                        fod << cityParms.city_n<< 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.org_city<< ',';
        fo << cityParms.org_cu << ',';
        fo << cityParms.x << ',';
        fo << cityParms.y << ',';
        fo << cityParms.pc << ',';
            fo << cityParms.n2 << ',';
            fo << cityParms.cc << ',';
            fo << cityParms.ty << ',';
            fo << '\t' << '\t' ;
            fo << cityParms.txt << '\t'<<'\t';
            fo << cityParms.org_n1 ;
        fo << '\n';
       
    }
   
}
static char clean(char c)
      {
            switch (c)
            {
            case '.' :
                  return ' ';
                  break;
            case '-' :
                  return ' ';
                  break;
        case '\'' :
                  return ' ';
                  break;

            case 'ü':
            case 'û':
                  return 'u';
                  break;
            case 'ö':
            case 'ó':
            case 'ô':
            case 'ø':
                  return 'o';
                  break;
            case 'ï':
            case 'î':
              return 'i';
                  break;
            case 'ñ':
                  return 'n';
                  break;
            case 'ë':
            case 'é':
            case 'è':
            case 'ê':
                  return 'e';
                  break;
            case 'À' :
            case 'Â' :
            case 'Ä' :  
                  return 'A';
                  break;
            case 'È':
            case 'É':
            case 'Ê':
        case 'Ë':
          return 'E';
              break;
        case 'Î':
          return 'I';
              break;
            case 'Ü':
                  return 'U';
                  break;
            case 'ß':
                  return 'B';
                  break;
        case 'Ô':
            case 'Ö':
          return 'O';
              break;
            case 'ã':
            case 'à':
            case 'â':
            case 'ä':
            case 'á':
          return 'a';
              break;
        case 'ç':
        return 'c';
              break;
              case 'ÿ':
        return 'y';
              break;
        default :
                  return tolower(c);
            }
      }


void main()
{
    CityParmsMap cmap;
    CityParmsArr carr;
    process_10_14(cmap);
    process_a_str(cmap, carr);
    str_out(carr, "c.txt");
}
0
 

Author Comment

by:girija_cv
ID: 10839140
what does this statement mean?

CityParms cityParms(x, y, loc, "", "", loc);
------------------------------------------------------------------------------------------
Using map.find I have coded as follows.But in the output I am not getting xvalue. How to get that?Please give me modified coding..

#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,org_city,n2,cc,ty;
    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 = "",const string org_city = "",const string n2 = "",const string cc = "",const string ty = "")
        : x(x1),y(y1),org_n1(org_n2),pc(pc1),city_n(city_n1),cu(cu1),txt(txt1),org_cu(org_cu1),strt(strt1),org_city(org_city),n2(n2),cc(cc),ty(ty) {}
   // CityParms(const CityParms& cp)
    //    : x(cp.x),y(cp.y),org_n1(cp.org_n1),pc(cp.pc),city_n(cp.city_n),cu(cp.cu),txt(cp.txt),org_cu(cp.org_cu),strt(cp.strt) {}
};

//typedef map<string, CityParms> CityParmsMap;
typedef multimap<std::string, CityParms> CityParmsMap;
typedef vector<CityParms*> CityParmsArr;
static char clean(char c);

void process_10_14(CityParmsMap& cmap)
{
   
    string fr_str,org_n1,loc,cu,x,y,pc,loc_poco,adm,cu_poco,org_cu;
      int i;
   
    ifstream fr("nz_add.dat");
    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,pc,',');
            getline(istr,loc,',');
                   
        org_n1 = loc;
       
       
            for(i=0;i<=loc.length();i++)
                  {
                        loc[i] = tolower(loc[i]) ;
                        if (loc[i] == '-')
                        {
                              loc[i] = ' ';
                        }
                        if (loc[i] == '\'')
                        {
                              loc[i] = ' ';
                        }
                  }
        loc_poco = loc + pc;
                  cout << loc_poco << endl;
                  CityParms cityParms;
                  cityParms.city_n = loc_poco;
                  cityParms.org_n1 = org_n1;
                  cityParms.pc = pc;
        cmap.insert(pair<string, CityParms>(loc_poco, cityParms));
            cout <<"address " << loc << pc <<endl;
    }
}
void process_a_str(CityParmsMap& cmap, CityParmsArr& carr)
{    
   
    std::ifstream f("nz_grd.dat");
      std::ofstream fod("out");
    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,cu,x,y,n2,cc,ty,city_poco,cu_poco,txt,org_city,org_cu;
            int j;
            getline(istr,city,',');
            getline(istr,cu,',');
            getline(istr,x,',');
            getline(istr,y,',');
            getline(istr,poco,',');
            getline(istr,n2,',');
            getline(istr,cc,',');
            getline(istr,ty,',');
            org_city = city;
            org_cu = cu;
                  for (j=0;j<=city.length();j++)
                  {
                        
                        city[j] = clean(city[j]);
                city[j] = tolower(city[j]);
                  }
                  for (j=0;j<=cu.length();j++)
                  {
                        
                        cu[j] = clean(cu[j]);
                cu[j] = tolower(cu[j]);
                  }
                  city_poco = city + poco;
                  cu_poco = cu + poco;
   
                  cout << "Processing address " << city_poco << '#' <<cu_poco << endl;
       
        CityParmsMap::iterator Citr;
        for (Citr = cmap.begin(); Citr != cmap.end(); ++Citr)
        {
            CityParms& cityParms  = Citr->second;
                  cout << "processing 10-14 cities " <<cityParms.city_n<< '#' << city_poco << endl;

            if (cityParms.city_n == city_poco)
            {  
                        txt = "Matched with loc";
                CityParms* pCityParms = new CityParms(org_city,org_cu,x,y,n2,cc,ty,txt,cityParms.org_n1);
                   
                carr.push_back(pCityParms);
                cout << pCityParms->org_n1 << cityParms.pc << "matched" << endl;
            }  
                  else
                  {
                        if (cityParms.cu == cu_poco)
                        {  
                              txt = "Matched with cu";
                CityParms* pCityParms = new CityParms(org_city,org_cu,x,y,n2,cc,ty,txt,cityParms.org_n1);
                   
                carr.push_back(pCityParms);
                //cout << pCityParms->city_n << pCityParms->strt<< cityParms.pc << "matched with cu" << endl;
                        cout << pCityParms->org_n1 << cityParms.pc << "matched" << endl;
                        }  
                        else
                        {
                        fod << cityParms.city_n<< 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.org_city<< ',';
        fo << cityParms.org_cu << ',';
        fo << cityParms.x << ',';
        fo << cityParms.y << ',';
        fo << cityParms.pc << ',';
            fo << cityParms.n2 << ',';
            fo << cityParms.cc << ',';
            fo << cityParms.ty << ',';
            fo << '\t' << '\t' ;
            fo << cityParms.txt << '\t'<<'\t';
            fo << cityParms.org_n1 ;
        fo << '\n';
       
    }
   
}
static char clean(char c)
      {
            switch (c)
            {
            case '.' :
                  return ' ';
                  break;
            case '-' :
                  return ' ';
                  break;
        case '\'' :
                  return ' ';
                  break;

            case 'ü':
            case 'û':
                  return 'u';
                  break;
            case 'ö':
            case 'ó':
            case 'ô':
            case 'ø':
                  return 'o';
                  break;
            case 'ï':
            case 'î':
              return 'i';
                  break;
            case 'ñ':
                  return 'n';
                  break;
            case 'ë':
            case 'é':
            case 'è':
            case 'ê':
                  return 'e';
                  break;
            case 'À' :
            case 'Â' :
            case 'Ä' :  
                  return 'A';
                  break;
            case 'È':
            case 'É':
            case 'Ê':
        case 'Ë':
          return 'E';
              break;
        case 'Î':
          return 'I';
              break;
            case 'Ü':
                  return 'U';
                  break;
            case 'ß':
                  return 'B';
                  break;
        case 'Ô':
            case 'Ö':
          return 'O';
              break;
            case 'ã':
            case 'à':
            case 'â':
            case 'ä':
            case 'á':
          return 'a';
              break;
        case 'ç':
        return 'c';
              break;
              case 'ÿ':
        return 'y';
              break;
        default :
                  return tolower(c);
            }
      }


void main()
{
    CityParmsMap cmap;
    CityParmsArr carr;
    process_10_14(cmap);
    process_a_str(cmap, carr);
    str_out(carr, "c.txt");
}
0
 

Author Comment

by:girija_cv
ID: 10839272
please ignore the previous coding consider the following code in which I have included map.find

#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) {}
    CityParms(const CityParms& cp)
        : x(cp.x),y(cp.y),org_n1(cp.org_n1),pc(cp.pc),city_n(cp.city_n),cu(cp.cu),txt(cp.txt),org_cu(cp.org_cu),strt(cp.strt) {}
};

//typedef map<string, CityParms> CityParmsMap;
typedef multimap<string, CityParms> CityParmsMap;
typedef vector<CityParms*> CityParmsArr;
multimap<string, CityParms>::iterator lastElement;
multimap<string, CityParms>::iterator itr;

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(x, y, loc, "", "", loc);
        cityParms.city_n = org_n1;
        cityParms.x = x;
        cityParms.y = y;
        cmap.insert(pair<string, CityParms>(loc, cityParms));
            cout << cityParms.city_n << '#' << cityParms.x << cityParms.y << endl;
    }
}
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::iterator Citr = cmap.find(city);
            if(Citr == cmap.end())
                  return;

                        lastElement = cmap.upper_bound(city);

      for ( ; Citr != lastElement; ++Citr)
      {
            
            CityParms& cityParms  = Citr->second;
            cout << "processing 10-14 cities " <<cityParms.city_n << endl;

                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");
}

This gives the o/p as follows
Auckland,Williams Avenue,,1747564,1706
Auckland,Williams Avenue,,100000,1706
Auckland,Willow Way,,1747564,1706
Auckland,Willow Way,,100000,1706
Auckland,Wyman Place,,1747564,1706
Auckland,Wyman Place,,100000,1706

Its not printing value of x. How to get this?
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 37 total points
ID: 10840324
That's my output using your last code...

Auckland,Williams Avenue,1747564,-368536,1706
Auckland,Williams Avenue,100000,-3556677,1706
Auckland,Willow Way,1747564,-368536,1706
Auckland,Willow Way,100000,-3556677,1706
Auckland,Wyman Place,1747564,-368536,1706
Auckland,Wyman Place,100000,-3556677,1706

You see, that it isn't 'x' that is missing but 'y'.

Try the following:

Copy the last posted code here from EE and paste it to a new text file. Name it city1.cpp for example.
Then remove your main cpp from your project and insert city1.cpp instead of it. Give feedback then,
whether the output still is wrong.

>> how to do it using map.find? instead of
>> if (cityParms.city_n == city)

Ok, you've found the solution... My documentation has a wrong description of multimap::upper_bound(), so i used a full loop instead.

>> what does this statement mean?
>>
>> CityParms cityParms(x, y, loc, "", "", loc);

Maybe, you found it also. It constructs a new object of CityParms that is used for insert in the multimap. I also needed a copy constructor for that operation as multimap::insert makes a copy of the inserted object. Look at the (single) map solution. There, we got the reference to a newly created empty CityParms object by  cmap[loc] (if key doesn't exist it creates a new empty data object and returns reference). After that we filled the empty object with the variables from the line currently read. However, that method won't work with multimap as it returns an iterator and not an object reference using find(). So i had to create an empty object myself, fill all parameters, insert it as a pair to cmap and the copy constructor fills the object that got stored in the multimap.

>>         CityParms* pCityParms = new CityParms(cityParms.x, cityParms.y, cityParms.org_n1, poco,
>>                                                                     city, cityParms.cu, cityParms.txt, cityParms.org_cu, strt);

Here we could use the copy constructor as well. However, the cityParms object has only the variables from a.txt, so it is easier to use the constructor where to pass all variables. BTW, if you still are wondering where the 'y' value is left: it must be here with that constructor that it goes wrong. Check with the debugger if either cityParms.x, cityParms.y are incorrect or if the new  pCityParms object got wrong values. Maybe your constructor is assigning the wrong values? Or your input file "a.txt" has too much commas. Take the 3 lines from above to check that.

Regards, Alex
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
Getting IP address 8 78
Would like to move button in a function 3 76
designing in object programming 12 76
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 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