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 the = symbol and == symbol?
What are near, far and huge pointers?
What are the properties of union in c?
What are the string functions? List some string functions available in c.
Differentiate between new and malloc(), delete and free() ?
Can an array be an Ivalue?
What is the difference between struct and typedef struct in c?
what are bit fields? What is the use of bit fields in a structure declaration?
What is the use of getch ()?
write a c program to find the sum of five entered numbers using an array named number
What is getch () for?
a parameter passed between a calling program and a called program a) variable b) constant c) argument d) all of the above
Is there anything like an ifdef for typedefs?
How can you tell whether two strings are the same?
Hai what is the different types of versions and their differences