• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

How to match duplicate cities in map?

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
girija_cv
Asked:
girija_cv
  • 4
  • 2
  • 2
2 Solutions
 
stefan73Commented:
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
 
stefan73Commented:
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
 
itsmeandnobodyelseCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
girija_cvAuthor Commented:
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
 
girija_cvAuthor Commented:
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
 
girija_cvAuthor Commented:
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
 
girija_cvAuthor Commented:
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
 
itsmeandnobodyelseCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now