3

I have a map nested inside another map, I want to assign values to the outer map, but i'm not quite sure how to do it. this causes the program to break even before it begins. I'm not showing any errors when I run it though

map<int, map<int, int>> outer;
map<int, int> inner;


outer.emplace(1, make_pair(2, 1));
outer.emplace(2, make_pair(2, 1));
outer.emplace(3, make_pair(2, 1));

outer.emplace(1, make_pair(3, 1));

Any help would help thanks

1 Answer 1

2

Well, your mapped_type for the outer map is map<int, int>, but you are trying to construct it with a pair<int, int>. You can try something like

outer.emplace(1, map<int,int>{ { 2, 1 } });
outer.emplace(2, map<int,int>{ { 2, 1 } });
outer.emplace(3, map<int,int>{ { 2, 1 } });

outer.emplace(1, map<int,int>{ { 3, 1 } });

That has the drawback that it is ugly, and it may not even be what you intended: The last line has no effect, because there is already a value for the key 1, and emplace has no effect in that case. If you meant to instead add the entry { 3, 1 } to the first inner map, such that it now contains { { 2, 1 }, { 3, 1 } }, you can instead use the following construct, which looks much nicer IMHO:

outer[1].emplace(2, 1);
outer[2].emplace(2, 1);
outer[3].emplace(2, 1);

outer[1].emplace(3, 1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.