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
Explain what is the benefit of using an enum rather than a #define constant?
What is a protocol in c?
What are the types of type specifiers?
What is use of #include in c?
What is type qualifiers?
List a few unconditional control statement in c.
What would happen to X in this expression: X += 15; (assuming the value of X is 5)
What are the benefits of organizational structure?
hi folks i m approching for h1 b interview on monday 8th of august at montreal and i m having little problem in my approval notice abt my bithdate my employer has made a mistake while applying it is 12th january and istead of that he had done 18 the of january do any body have any solution for that if yes how can i prove my visa officer abt my real birthdate it urgent please let me know guys thaks dipesh patel
What is the process to generate random numbers in c programming language?
Which of the following operators is incorrect and why? ( >=, <=, <>, ==)
Explain about C function prototype?
Whats s or c mean?
What is the use of typedef in structure in c?
How do you override a defined macro?