A map inside a map

Oct 13, 2014 at 10:59am
Hello.

How do you store a map value inside a map?

Here's my code:

 
	map<map<string, string>, int> Name;
Last edited on Oct 13, 2014 at 10:59am
Oct 13, 2014 at 11:02am
Before I answer your question, I need to know what are you trying to achieve by this.
Oct 13, 2014 at 11:12am
Well, I just wanted to know how to put values. I'm thinking of having a map where you have a name seperated into 2 strings(last name, first name) using map and having an extension of their age.
Oct 13, 2014 at 11:15am
You can try map<string, map<string, int>>, but boost::multi_index_container suits that better.

Or have a map<pair<string, string>, int>. Every approach has drawbacks depending on what are you trying to do.
Last edited on Oct 13, 2014 at 11:17am
Oct 13, 2014 at 11:18am
How is map<string,map<string, int>> different from map<map<string, string>, int>?
Oct 13, 2014 at 11:28am
Remember, that map keys are (a) constant, (b) goes inside [].

And if we take into account how maps store key-value pairs, we will see that it makes it poor choice for keys... Well, there is a good rule of thumb: do not use containers as keys for associative containers.

Basicly you can have some unexpected things as
1
2
3
bad_map[{{"Bill", Smith}}] = 10;
bad_map[{{"john", Smith}}] = 11;
bad_map[{{"john", Smith}, {"Bill", Smith}}] = 12;
Yes. That three different pair of key/value.
Oct 13, 2014 at 12:12pm
Okay. I get now thank you.
Topic archived. No new replies allowed.