Solved

how to get uniq records in c++

Posted on 2004-04-02
6
333 Views
Last Modified: 2010-04-01
Hi,

I have one file, which looks as below,

 x.txt

abcd,123
abcd,1234
abcd,1235
abcd,1236
acd,131
acd,132
acd,133
abd,231
abd,232
abd,233

I need to print the unique name along with all its values. I have perl script I want to code it in c++
how to do it?

I need to print the o/p as follows.
eg.
abcd,123,1234,1235,1236
abd,231,232,233
acd,131,132,133

Perl code is as follows

while (<>)
{
        chop;
        ($loc,$xy) = split(/,/,$_);
        push @{$MY{$loc}},$xy;
}
foreach $loc (sort keys (%MY))
{
        #print $loc . "-" . join(" ",@{$MY{$loc}}) . "\n" if ($loc);
        print "$loc,@{$MY{$loc}}\n" if ($loc);
}


c++ coding is as follows but it is not working properly.Can any one solve this problem?


#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <list>
using namespace std;

 struct CityParms {
        std::string loc,poco;
        std::list<std::string> xy_list;
            CityParms(const std::string& poco = "",const std::string& loc = "") : poco(poco),loc(loc){}
};
typedef std::map<std::string,CityParms> CityParmsMap;

void process_a(CityParmsMap& map);
void report(const CityParmsMap& map);


int main()
{
        CityParmsMap map;
        process_a(map);
            report(map);

}

void process_a(CityParmsMap& map)
{
        std::ifstream f("x.txt");
        std::string str;
        while (!f.eof())
            {
                        getline(f,str);
                std::istringstream istr(str);
                std::string str,poco,loc;
                getline(istr,loc,',');
                        getline(istr,poco,',');
                        
                        cout << loc << '#' << poco << endl;
                map[loc] = CityParms(poco);
                        CityParmsMap::const_iterator Citr = map.find(loc);
                                    if(Citr != map.end())
                                    {
                                    CityParms& cityParms = map[loc];
                                    cityParms.loc = loc;
                        cityParms.xy_list.push_back(poco);
                                    }
                        
            }
}

void report(const CityParmsMap& map)
{
        std::ofstream fo("push_out.dat");
        typedef CityParmsMap::const_iterator Citr;
        for (Citr itr = map.begin();itr != map.end();++itr) {
               
                const CityParms& cityParms = itr->second;
                typedef std::list<std::string>::const_iterator Citr;
                const std::list<std::string> &xy_list = cityParms.xy_list;
                        
                        if (!xy_list.size())
                              continue;
                               fo << cityParms.loc << ',';
                               copy (xy_list.begin(),xy_list.end(),std::ostream_iterator<std::string>(fo));
                              
               
             
        }
}

0
Comment
Question by:girija_cv
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10739970
Hi girija_cv,
> typedef std::map<std::string,CityParms> CityParmsMap;

Use a simpler approach:

typedef std::map<std::string,vector<std::string> > CityParmsMap;

Then you'll have a very similar behaviour as your Perl script.


Cheers,
Stefan
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10740077
Try this:

    map<string, list<int>* >  cmap;

    ifstream ifs("x.txt");
    string line, key;
    int    num;

    map<string, list<int>* >::iterator itr;

    while (!ifs.eof())
    {
        getline(ifs, line);
       
        if (line.empty())
            break;
        istringstream iss1(line);
        getline(iss1, key, ',');
        iss1 >> num;  
       
       
        if ((itr = cmap.find(key)) == cmap.end())
        {
            list<int>* plist = new list<int>;
            plist->push_back(num);
            cmap[key] = plist;
        }
        else
        {
            list<int>*& plist = cmap[key];
            plist->push_back(num);
        }
   }

   for (itr = cmap.begin(); itr != cmap.end(); ++itr)
   {
       pair< string, list<int>* > pr = *itr;
       key  = pr.first;
       list<int>* pl = pr.second;
       
       cout << key ;  
       list<int>::iterator it;
       for (it = pl->begin(); it != pl->end(); ++it)
           cout << "," << *it;
       cout << endl;
   }

   ifs.close();


Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10740093
if you want to sort them add sort() function when outputting the results

       list<int>* pl = pr.second;
       pl->sort();

Regards, Alex
 
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 17

Accepted Solution

by:
rstaveley earned 20 total points
ID: 10740096
Looks like we all had a go at this at the same time. Alex and Stefan must be avoiding whatever project that they are supposed to be working on in the real world too ;-)

--------8<--------
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <list>
#include <sstream>
#include <iterator>
#include <algorithm>

int main()
{
        using std::string;
        typedef std::list<string> ParmsList;
        typedef std::map<string,ParmsList> CityMap;
        CityMap cityMap;

        std::ifstream fin("x.txt");
        string line;
        while (getline(fin,line)) {
                std::istringstream istr(line);
                string city,parm;
                if (getline(istr,city,',') && getline(istr,parm,','))
                        cityMap[city].push_back(parm);
        }

        typedef CityMap::const_iterator CMCIter;
        for (CMCIter itr = cityMap.begin();itr != cityMap.end();++itr) {
                std::cout << itr->first << ',';
                const ParmsList& parmsList = itr->second;
                copy(parmsList.begin(),parmsList.end(),std::ostream_iterator<string>(std::cout,","));
                std::cout << '\n';
        }
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10740103
[Shouldn't have posted really, but having done all of that typing, I couldn't bear the thought of leaving it.]
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10740163
>>whatever project that they are supposed to be working on in the real world too

project?? real world?? what are you talking about?

- sardonic grin - ;-)

Regards, Alex
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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