Printing std::map contents differently

The exercise (8 of chapter 21 of the book PPP) says:
Take the word-frequency example from ยง21.6.1 and modify it to output its lines in order of frequency (rather than in lexicographical order). An example line would be 3: C++ rather than C++: 3.

I'm not very sure about the exact intention of the exercise, but one code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <map>
#include <string>

int main()
{
	std::map<std::string,int> words;
	std::cout << "Enter string values for the map. Enter ctrl+z at the end.\n";

	for (std::string s; std::cin >> s;)
		++words[s];

	// To get rid of ctrl+z flags settings
	std::cin.ignore(INT_MAX, '\n');
	std::cin.clear();

	std::map<int, std::string> nums;
	for (const auto& w : words) 
	   nums.insert({ w.second, w.first });

	std::cout << "The values based on frequency order are\n";
	for (auto iter = nums.rbegin(); iter != nums.rend(); ++iter)
		std::cout << iter->first  << ": " << iter->second << '\n';
	std::cout << '\n';

	return 0;
}


The output for the input: one three two three is:
2: three
1: one

which is logical since the key in std::map is unique. But I assume that the intention has been something like:
2: three
1: one
1: two

for the output. What are your ideas about the exercise, please?
Of course, I can use two vectors one for storing int and the other for storing string values and play with them so that the latter output is printed as the result, but that'd increase the complexity of the project unreasonably likely!

Mostly when I'm coding for an exercise I encounter this trouble that what is the intention of the exercise or what result is meant by it exactly!
Last edited on
Line 19: insert(...) does not overwrite an existing value, hence you get always the first inserted value.

The operator [] does overwrite the value, so consider:

nums[w.second] += w.first + ' ';

Output:
2: three 
1: one two 
Cool!
I didn't know we can add to a map its second elements that way and it's still a shade strange for me.
"...it's still a shade strange for me."

The state of mind one can expect to experience just before you really learn something is confusion.

So, one must develop the expectation that confusion represents that moment just before you figure something out. It's a symptom resulting from the effort of the mind to surround the problem, becoming aware of what is not understood, where before there was the bliss of ignorance. It just so happens the brain itself was built to work through this situation, and tends to do it with some automatic response given patience and persistence.

As the decades pile on this becomes the way of life.
Enter string values for the map. Enter ctrl+z at the end.

So, one must develop the expectation that confusion represents that moment just before 
you figure something out. It's a symptom resulting from the effort of the mind to surround 
the problem, becoming aware of what is not understood, where before there was the bliss 
of ignorance. It just so happens the brain itself was built to work through this situation, and 
tends to do it with some automatic response given patience and persistence.

^Z

The values based on frequency order are
6: the
3: of to
2: and before just that was
1: It It's So, a automatic aware becoming bliss brain built confusion develop do effort 
expectation figure from given happens ignorance. is it itself mind moment must not one
out. patience persistence. problem, represents response resulting situation, so some 
something surround symptom tends there this through understood, what where with work 
you
Last edited on
aha, I found it. nums[w.second] is indeed a string!

Isn't that subscribe operator a clue for using random access iterators? As far as I'm concerned only std::vector is provided with that type of iterator.
I'm not exactly sure what you mean, but the operator[] for std::map is not random access, it uses some tree-like structure and is O(log n) instead of O(1) like a vector.
Topic archived. No new replies allowed.