-
insert map into map?
Im having problems inserting a map into a map, I cant manage to access the actual element and call insert() on it.
Thank you
Code:
#include <map>
using namespace std;
int main() {
map<int, multimap<int, int> > my_map;
map<int, multimap<int, int> >::iterator my_it;
pair<map<int, multimap<int, int> >::iterator, bool> func_pair;
my_it = my_map.find(42);
if(my_it == my_map.end()) {
func_pair = my_map.insert(make_pair(42, multimap<int, int>()));
(func_pair.first)->insert(make_pair(42, 42));
} else {
//
}
return 0;
}
-
func_pair.first is an iterator into your map. Dereferencing that iterator will give you a pair of <int, multimap>. If you are intending to insert your (42, 42) pair into that multimap, you will have to access the second element of that pair, something like (untested):
Code:
((func_pair.first)->second).insert(make_pair(42, 42));
-
What problem are you trying to solve such that you've arrived at a map to a multimap as the best solution? That's awfully unusual.
Or is it just a learning exercise?