Re: C++ solution for K & R(2nd Ed) Ex.6-4 - better solution needed
On 2007-09-30 04:37, email@example.com, India wrote:
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.
It is a good solution, however it does not take full advantage of the
containers provided in the standard library. For example unique_words
should probably be a set instead of a vector. Using the vector to store
both frequency and word have some advantages over a map, since it can be
sorted by both frequency and word making it useful in both stages of the
program. My only complaint is that your solution requires you to write
more code than one that takes better advantage of the standard
containers, it will however probably perform better and sometimes that
is more important.
My suggestion would have been to use first a map to read in the words
and counting their frequency, and then construct a multimap sorted by
frequency which is then printed:
std::map<std::string, size_t> words;
// Read words and count frequency
while (std::cin >> w)
// Construct multimap, notice the use of greater to sort it in
// decending order instead of ascending
std::multimap<size_t, std::string, std::greater<size_t> > freq;
std::map<std::string, size_t>::iterator i;
for (i = words.begin(); i != words.end(); ++i)
// Print, someone can probably come up with a way to use std::copy
// to print it instead, but I prefer an explicit loop
std::multimap<size_t, std::string, std::greater<size_t>>::iterator j;
for (j = freq.begin(); j != freq.end(); ++j)
std::cout << j->first << "\t" << j->second << "\n";