View Single Post
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Determining the number of unique words in a .txt file

 
0
  #3
Dec 3rd, 2008
I think you should look at the below code more carefully and must understand the complexity and map datastructure,

if you've any confusion post, I will explain you in more detail then.
  1. fstream inFile("C:\\Laiq.txt");
  2.  
  3. string line;
  4. std::map<string, int> mapWordCount;
  5. string word;
  6. std::string::size_type endWord;
  7. std::string::size_type startWord;
  8. while (std::getline (inFile,line)) {
  9. startWord = 0;
  10. while ((endWord = line.find_first_of(" ", startWord))!= line.npos)
  11. {
  12. word = line.substr (startWord, endWord - startWord);
  13. mapWordCount[word]++;
  14. startWord = endWord+1;
  15. }
  16. }
  17. inFile.close();
  18.  
  19.  
  20. for(map<string,int>::const_iterator iter = mapWordCount.begin();
  21. iter != mapWordCount.end(); ++iter) {
  22. cout<< "The Frequency of word: "<< iter->first << " is : "<< iter->second <<endl;
  23. }
Last edited by Laiq Ahmed; Dec 3rd, 2008 at 5:06 am. Reason: Changing Code Intendation
Reply With Quote