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 meant by gets in c?
int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above
What is a structure member in c?
How are structure passing and returning implemented?
Explain what are linked list?
C program to find all possible outcomes of a dice?
Sir i need notes for structure,functions,pointers in c language can you help me please
What does the message "automatic aggregate intialization is an ansi feature" mean?
When should we use pointers in a c program?
write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1
What is meant by preprocessor in c?
How do you search data in a data file using random access method?
How do you construct an increment statement or decrement statement in C?
Why is not a pointer null after calling free?
What is a null pointer assignment error? What are bus errors, memory faults, and core dumps?