| | |
problem with a list!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 13
Reputation:
Solved Threads: 0
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 !!!!!!!!!!!!!
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 !!!!!!!!!!!!!
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
// 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 }
Perhaps this
And using that:
eek.txt
my output:
And using that:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
chump hoe fag faggot chump fool yo momma
my output:
C++ Syntax (Toggle Plain Text)
chump 2 fag 1 faggot 1 fool 1 hoe 1 momma 1 yo 1
*Voted best profile in the world*
![]() |
Similar Threads
- Doubly Linked List Problem (C++)
- BIG problem with STL List Container (C)
- Passing a drop down list item's value (HTML and CSS)
- Weird (?) problem using std::list ... (C++)
- hi problem in dropdown list (ASP)
- Linked List & Objects (C++)
- Ginormous Problem With XP (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: A couple Qs (arrays, c++, and reading in text files)
- Next Thread: Make .exe files that runs on any machine with VS C++ 6.0?
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






