Link to home
Start Free TrialLog in
Avatar of girija_cv
girija_cv

asked on

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?
Avatar of stefan73
stefan73
Flag of Germany image

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
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ;-)

Avatar of girija_cv
girija_cv

ASKER

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;
        }    
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");
}
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");
}
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial