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
using for loop sum 2 number of any 4 digit number in c language
What are qualifiers in c?
How to write a multi-statement macro?
write a program to concatenation the string using switch case?
What does void main () mean?
Explain bit masking in c?
What is static identifier?
main() { int i = 10; printf(" %d %d %d ", ++i, i++, ++i); }
What is wrong in this statement?
Why c language?
Is this program statement valid? INT = 10.50;
What is calloc() function?
What are dangling pointers in c?
Define macros.
please send me the code for multiplying sparse matrix using c