Thread: insert map into map?

  1. #1
    Registered User
    Join Date
    May 2006

    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

    #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;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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):
    ((func_pair.first)->second).insert(make_pair(42, 42));

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Map insert not working?
    By JMK in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2010, 01:20 AM
  2. insert
    By nirnir26 in forum C Programming
    Replies: 6
    Last Post: 11-26-2009, 08:14 AM
  3. std::insert
    By Coding in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2008, 02:08 PM
  4. insert another exe into exe
    By lliero in forum C Programming
    Replies: 8
    Last Post: 04-12-2002, 12:22 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM