Solved

how to get uniq records in c++

Posted on 2004-04-02
6
329 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now