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.
fstream inFile("C:\\Laiq.txt");
string line;
std::map<string, int> mapWordCount;
string word;
std::string::size_type endWord;
std::string::size_type startWord;
while (std::getline (inFile,line)) {
startWord = 0;
while ((endWord = line.find_first_of(" ", startWord))!= line.npos)
{
word = line.substr (startWord, endWord - startWord);
mapWordCount[word]++;
startWord = endWord+1;
}
}
inFile.close();
for(map<string,int>::const_iterator iter = mapWordCount.begin();
iter != mapWordCount.end(); ++iter) {
cout<< "The Frequency of word: "<< iter->first << " is : "<< iter->second <<endl;
}
Last edited by Laiq Ahmed; Dec 3rd, 2008 at 5:06 am. Reason: Changing Code Intendation