hello,
how do i remove elements which are repeated in a list .I cant seem to do it . in this program i am first putting elements into the list ,then searching for a particular element seeing how many times it occurs in the list(count) .i need to display the word and the count . but i need to eliminate the alias words in the list .am so confused that now i cant even trace the program . pls help !!!!!!!!!!!!!

#include<iostream>
#include<conio.h>
#include<fstream>
#include<list>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
    //void fi(int count,string word);
    int cnt;
    string key;
    string word;
    char line[80];
    char line1[80];
    list<string> lst;
    list<string>::iterator p;
    string word1;
    int n;
    map<string,int> m;
    ifstream in ("d:/programming/count.txt");
    if(!in)
    {
           cout<<"cannot open count"<<endl;
    }
    ofstream out ("d:/programming/count1.txt");
    if(!out)
    {
           cout<<"cannot open count1"<<endl;
    }
 
    do{
             in.getline(line,80);
             word=line;
             lst.push_back(word);
 
    }while(in);
    p=lst.begin();
    while(p != lst.end())
    {
         lst.sort();//sort the list
         out<<*p<< " " << endl;//put the contents into the final file
         p++;
    }
    ifstream in1 ("d:/programming/count.txt");
    if(!in1)
    {
           cout<<"cannot open count"<<endl;
    }
    ofstream out1 ("d:/programming/count.txt");
    if(!out1)
    {
           cout<<"cannot open count1"<<endl;
    }
    cout<<"do it"<<endl;
 
    do{
             in1.getline(line,80);
             word=line;
             out1<<word<<endl ;            
             cnt=count(lst.begin(),lst.end(),word);
             out1<<cnt;
             for(cnt;cnt>1;cnt--)
             {
                                 lst.pop_front();
 
             }        
    }while(in1);
 
    getch();
    return 0;
}
/*void fi(int count,string word)
{
     char line[80];
     list<string> lst;
    ifstream in2 ("d:/programming/count1.txt");
    if(!in2)
    {
           cout<<"cannot open count"<<endl;
    }
    do{
             in2.getline(line,80);
             word=line;
             cout<<"sec "<<word<<endl;
             lst.push_back(word);
    }while(in2);
     list<string>::iterator p;
     p=find(lst.begin(),lst.end(),word);
     for(int i=count ; i<2;i--)
     {
                 lst.pop_front();
                 cout<<count<<endl;
     }
     while(p != lst.end())
    {
         cout<<*p<< " " << endl;//put the contents into the final file
         p++;
    }
}*/

You don't need that list at all. you alreay have map which you can use to map strings with their counts. Add the words to the map when read and you will not need the list at all. Also, the loop to read the file can be coded better

// read the file one word at a time.  If the word contains any 
// punctuation, such as commas and periods you will probably want to 
//remove then before adding it to the map.
   while( in >> word )
   {
          // add the word to the map
   }

thankx !! one question .. elements wont be repeated in a map is it?? for example if i ve 3 elements say "how" ..then in a map wil it be stored as
how 3
how 3
how 3
or wil it be stored only once??

Member Avatar for iamthwee

Perhaps this

And using that:

#include <iostream> 
#include <map> 
#include <string> 
#include <fstream>

using namespace std; 
int main() 
{ 
        ifstream in ("c:/eek.txt");
        
        std::map<string, int> count; 
        string s; 
        
        while( in >> s )
        {
            ++count[s]; 
        }  
        in.close();
        
        std::map<string, int>::iterator it; 
        for (it = count.begin(); it != count.end(); ++it) 
        cout << it->first << "\t" << it->second << endl; 
        
        cin.get();
}

eek.txt

chump
hoe
fag
faggot
chump
fool
yo momma

my output:

chump   2
fag     1
faggot  1
fool    1
hoe     1
momma   1
yo      1

thankx a ton

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.