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 the = symbol and == symbol?

1072


What are near, far and huge pointers?

1030


What are the properties of union in c?

1026


What are the string functions? List some string functions available in c.

957


Differentiate between new and malloc(), delete and free() ?

1153


Can an array be an Ivalue?

1069


What is the difference between struct and typedef struct in c?

1101


what are bit fields? What is the use of bit fields in a structure declaration?

2077


What is the use of getch ()?

1053


write a c program to find the sum of five entered numbers using an array named number

2094


What is getch () for?

1175


a parameter passed between a calling program and a called program a) variable b) constant c) argument d) all of the above

1087


Is there anything like an ifdef for typedefs?

1122


How can you tell whether two strings are the same?

1224


Hai what is the different types of versions and their differences

1915