Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is the difference between Printf(..) and sprint(...) ?

1522


What does the function toupper() do?

1119


What are # preprocessor operator in c?

1104


What are logical errors and how does it differ from syntax errors?

1293


What is wrong in this statement? scanf(ā€œ%dā€,whatnumber);

1234


Are the expressions * ptr ++ and ++ * ptr same?

1145


What are the advantages of using macro in c language?

1119


how to introdu5ce my self in serco

2003


What are keywords c?

1027


Can main () be called recursively?

1115


What are register variables in c?

1009


How to Throw some light on the splay trees?

1059


Is Exception handling possible in c language?

2045


how can use subset in c program and give more example

1989


What is string in c language?

1145