Write the program for displaying the ten most frequent words
in a file such that your program should be efficient in all
complexity measures.
Answer Posted / mayank maheshwari
Hi,
you can do it in a cleaner way using STL map and getting rid
of all the messy strtok() functions. Just writing how to
construct the map. The idea of putting into a vector and
sorting can still work.
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main(int argc,char*argv[])
{
string word;
map <string, int> freq;
map <string, int>::const_iterator wordsit;
fstream myfile;
myfile.open(argv[1],ios::in);
// Load file into string
if (myfile.is_open())
{
while (myfile >> word)
{ freq[word]++;
}
myfile.close();
}
//To see the map created
for (wordsit=freq.begin();wordsit!=freq.end();wordsit++)
{cout<<"Key: "<<wordsit->first<<"Value:
"<<wordsit->second<<endl;
}
| Is This Answer Correct ? | 3 Yes | 5 No |
Post New Answer View All Answers
What is the difference between Printf(..) and sprint(...) ?
What does the function toupper() do?
What are # preprocessor operator in c?
What are logical errors and how does it differ from syntax errors?
What is wrong in this statement? scanf(ā%dā,whatnumber);
Are the expressions * ptr ++ and ++ * ptr same?
What are the advantages of using macro in c language?
how to introdu5ce my self in serco
What are keywords c?
Can main () be called recursively?
What are register variables in c?
How to Throw some light on the splay trees?
Is Exception handling possible in c language?
how can use subset in c program and give more example
What is string in c language?