Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to get uniq records in c++

Posted on 2004-04-02
6
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
rstaveley earned 80 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

719 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