1

I'm trying to use a map within a map using the following code;

 map<map <int,int>,int > multimap;

Now I want to insert the data such that multimap[1]--->(2,3), multimap[4]--->(5,6). I have been trying to insert it the following way but i guess there is a syntax error which I can't seem to figure out!

 multimap.insert(pair<int,pair<int,int>(2,3)>(1));

Any help will be appreciated. And after adding, how would I display the values by using an iterator?

10
  • 3
    Would you like to share what the syntax error is? Commented Apr 19, 2012 at 16:13
  • error: a call to a constructor cannot appear in a constant-expression error: template argument 2 is invalid Commented Apr 19, 2012 at 16:15
  • 3
    From your description of what you want to map, wouldn't you want something more like: map<int, pair<int, int>> multimap since you want a single value that maps to a pair of values? Commented Apr 19, 2012 at 16:17
  • for one thing, you're keying a multimap with a map... I don't understand why you'd do that, since your key needs a < operator. Commented Apr 19, 2012 at 16:17
  • 1
    Did I get this right? This is not a multimap, it's just a map called multimap right? I don't know why you'd do that. Commented Apr 19, 2012 at 16:26

4 Answers 4

4

The map template calls for two parameters map<key, value>. The key has to have some way of comparing itself with other keys. In map it uses the < operator, so if you key with a map<int,int> you're going to have problems, since there's no defined < for an arbitrary map.

Try

map<int, map<int, int> > multimap; 
Sign up to request clarification or add additional context in comments.

4 Comments

I wouldn't call the variable multimap though... just asking for trouble.
can you please tell me how to insert using the insert function?
why don't you just use multimap[2][3] = 1?
it's working. But can we do it with an insert function? or is there a certain limitation to it?
2

If you want a multipmap then you need to use a multimap :) Here is declaration and an insert statement.

typedef pair<int, int> valPair;
multimap<int, valPair> mmap;

mmap.insert(make_pair(1, make_pair(2, 3)));
mmap.insert(make_pair(1, make_pair(4, 5)));

for(multimap<int, valPair>::iterator it = mmap.begin();
    it != mmap.end(); ++it)
{
    cout << "Key: " << it->first << " Vals:(";
         << it->second.first << ", " << it->second.second << ")" << endl;
}

2 Comments

@AmaarBokhari updated with the iteration. Just hadn't copied it over yet.
yeah i noticed...but thanks again...new to C++'s STL library! :)
0

It looks to me like you're confusing maps and multimaps, and keys and values.

If you want a map that maps the key '1' to the values '(2,3)' and '(4,5)' then do this:

multimap<int,pair<int,int>> myMap { {1,{2,3}}, {1,{4,5}} };

or in C++03:

multimap<int,pair<int,int> > myMap;
myMap.insert(make_pair(1,make_pair(2,3)));
myMap.insert(make_pair(1,make_pair(4,5)));

When iterating over the map each item refers to a pair where the first item of the pair is the key and the second item is the value.

for(auto &i : myMap) {
    std::cout << "Key: " << i->first
      << " Value: (" << i->second.first << ',' << i->second.second << ")\n";
}

Or in C++03

for(multimap<int,pair<int,int> >::const_iterator i(myMap.begin()),end(myMap.end()); i!=end; ++i) {
    std::cout << "Key: " << i->first << " Value: (" << i->second.first << ',' << i->second.second << ")\n";
}

This would print out:

Key: 1 Value: (2,3)
Key: 1 Value: (4,5)

Comments

0

-Firstly, there's no multimap in your code - there is simply a map called 'multimap'.

-Secondly, the structure

map< map<int, int>, int > multimap;

will not map 1-->2, 3, because you're using a map as a key, and an int as the value.

-Thirdly, the reason you're getting a build error is because the insert() method in your case takes

pair< map<int, int>, int >

What you probably want is this:

map< int, pair<int, int> > multimap;
pair<int, int> myPair( 2, 3 );
multimap.insert( pair< int, pair<int, int> >( 1, myPair ) );

map< int, pair<int, int> >::iterator iter = multimap.begin();
while( iter != multimap.end() )
{
    int key = iter->first;
    pair<int, int> aPair = iter->second;
    ++iter;
}

2 Comments

@AmaarBokhari I'm afraid I've cheated and edited the answer while you were replying - I had a clot on the keyboard! :-) To iterate the map you want to look at the begin() and end() functions, and look at iterators; particularly retrieving one from begin(), incrementing it with ++, and comparing it to end().
@AmaarBokhari Don't be shy about voting me up. ;-)

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.