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
An expression to whose value an operater is applied a) operand b) variable c) constant d) all of the above
What is #define?
What is the value of c?
in any language the sound structure of that language depends on its a) character set, input/output function, its control structures b) character set, library functions, input/output functions its control structures c) character set, library functions, control sturctures d) character set, operators, its control structures
Explain how are portions of a program disabled in demo versions?
What is the difference between a function and a method in c?
Difference between goto, long jmp() and setjmp()?
How do I create a directory? How do I remove a directory (and its contents)?
What is function in c with example?
What is c language & why it is used?
What do you mean by Recursion Function?
Can a pointer be volatile in c?
write a program which the o/p should b in such a way that s triangle if I/p is 3,a Square/rectangle if I/P=4,a pentagon if I/P=5 and so on...forget about the I/P which is less than 3
Explain void pointer?
What is pointer to pointer in c language?