problem with a list!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

problem with a list!!

 
0
  #1
May 14th, 2006
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 !!!!!!!!!!!!!
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<fstream>
  4. #include<list>
  5. #include<algorithm>
  6. #include<map>
  7. using namespace std;
  8. int main()
  9. {
  10. //void fi(int count,string word);
  11. int cnt;
  12. string key;
  13. string word;
  14. char line[80];
  15. char line1[80];
  16. list<string> lst;
  17. list<string>::iterator p;
  18. string word1;
  19. int n;
  20. map<string,int> m;
  21. ifstream in ("d:/programming/count.txt");
  22. if(!in)
  23. {
  24. cout<<"cannot open count"<<endl;
  25. }
  26. ofstream out ("d:/programming/count1.txt");
  27. if(!out)
  28. {
  29. cout<<"cannot open count1"<<endl;
  30. }
  31.  
  32. do{
  33. in.getline(line,80);
  34. word=line;
  35. lst.push_back(word);
  36.  
  37. }while(in);
  38. p=lst.begin();
  39. while(p != lst.end())
  40. {
  41. lst.sort();//sort the list
  42. out<<*p<< " " << endl;//put the contents into the final file
  43. p++;
  44. }
  45. ifstream in1 ("d:/programming/count.txt");
  46. if(!in1)
  47. {
  48. cout<<"cannot open count"<<endl;
  49. }
  50. ofstream out1 ("d:/programming/count.txt");
  51. if(!out1)
  52. {
  53. cout<<"cannot open count1"<<endl;
  54. }
  55. cout<<"do it"<<endl;
  56.  
  57. do{
  58. in1.getline(line,80);
  59. word=line;
  60. out1<<word<<endl ;
  61. cnt=count(lst.begin(),lst.end(),word);
  62. out1<<cnt;
  63. for(cnt;cnt>1;cnt--)
  64. {
  65. lst.pop_front();
  66.  
  67. }
  68. }while(in1);
  69.  
  70. getch();
  71. return 0;
  72. }
  73. /*void fi(int count,string word)
  74. {
  75.   char line[80];
  76.   list<string> lst;
  77.   ifstream in2 ("d:/programming/count1.txt");
  78.   if(!in2)
  79.   {
  80.   cout<<"cannot open count"<<endl;
  81.   }
  82.   do{
  83.   in2.getline(line,80);
  84.   word=line;
  85.   cout<<"sec "<<word<<endl;
  86.   lst.push_back(word);
  87.   }while(in2);
  88.   list<string>::iterator p;
  89.   p=find(lst.begin(),lst.end(),word);
  90.   for(int i=count ; i<2;i--)
  91.   {
  92.   lst.pop_front();
  93.   cout<<count<<endl;
  94.   }
  95.   while(p != lst.end())
  96.   {
  97.   cout<<*p<< " " << endl;//put the contents into the final file
  98.   p++;
  99.   }
  100. }*/
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problem with a list!!

 
0
  #2
May 15th, 2006
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
  1. // read the file one word at a time. If the word contains any
  2. // punctuation, such as commas and periods you will probably want to
  3. //remove then before adding it to the map.
  4. while( in >> word )
  5. {
  6. // add the word to the map
  7. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

Re: problem with a list!!

 
0
  #3
May 15th, 2006
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??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: problem with a list!!

 
0
  #4
May 15th, 2006
Perhaps this

And using that:

  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. int main()
  8. {
  9. ifstream in ("c:/eek.txt");
  10.  
  11. std::map<string, int> count;
  12. string s;
  13.  
  14. while( in >> s )
  15. {
  16. ++count[s];
  17. }
  18. in.close();
  19.  
  20. std::map<string, int>::iterator it;
  21. for (it = count.begin(); it != count.end(); ++it)
  22. cout << it->first << "\t" << it->second << endl;
  23.  
  24. cin.get();
  25. }

eek.txt
  1. chump
  2. hoe
  3. fag
  4. faggot
  5. chump
  6. fool
  7. yo momma

my output:
  1. chump 2
  2. fag 1
  3. faggot 1
  4. fool 1
  5. hoe 1
  6. momma 1
  7. yo 1
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

Re: problem with a list!!

 
0
  #5
May 15th, 2006
thankx a ton
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC