Home

First I express my thanks to all of you for replying.

Looks like, some of you have suggested map data structure. This cannot
be used because it sorts the input based on string which is the key.
But the input words should not be sorted. Their order should be
retained as they appear in the input. Later, they should be sorted
based on frequency of their occurrence.

Let me put the logic that I have used, in words.
Instead of going through the code, kindly go through this and give me
your feedback as help.

I create "vector<string> unique_words;" to store each input word as it
arrives(after checking if it is already not found in this vector). I
also create "vector< pair<int, string> > v;" along with the above
vector<string>. Whenever a word arrives, first it is stored in
vector<string> if it is a new word and in this case, make_pair(1,
word) is stored in vector<pair<int,string>>. If the word has been
previously stored, then its count is incremented in
vector<pair<int,string>>. After reading all words, I do

sort(v.begin(), v.end(), cmp_fn);

for_each(v.begin(), v.end(), print);

The disadvantage is the addition of two global functions cmp_fn and
print. There can be other disadvantages also.

Kindly give your feedback.

Thanks
V.Subramanian

previous
next

Re: __contains__() : Bug or Feature ???
Re: placement new
signed/unsigned wchar_t
Re: Python XML API's that preserve XML comments?
Re: Stream states questions
Fundacja Sloneczko
Dzieci Niczyje
Nasze Dzieci
Podaruj Zycie
Mimo Wszystko