0

Found this Multimap containing pairs?, but it is not much help

How would I insert two strings into pair? Below, my two failed attempts.

multimap<string, pair<string,string> > mymm;
mymm["Alex"] = std::pair<"000","000">; //errors
mymm.insert(pair<string, pair<string, string> > 
           ("Alex", std::pair<"000","000">); // errors out as well

I am using Visual Studio 2010, 32 bit. Thanks !

Community
  • 1
  • 1
newprint
  • 6,936
  • 13
  • 67
  • 109

2 Answers2

5
mymm.insert(make_pair("Alex",make_pair("000","000")));

A multimap doesn't allow lookup using operator [], since there may be more than one match.

make_pair is a convenient way to create a pair without having to specify the types explicitly. Without using make_pair, you would need to do something like this:

mymm.insert(pair<string,pair<string,string> >("Alex",pair<string,string>("000","000")));
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • Thanks! `map` uses `[]` operator to insert the values. I was thinking `multimap` behaves the same way. – newprint Jun 28 '12 at 04:39
  • 1
    It might be worth explaining the actual error he gets from calling std::pair<"000","000">; he's mixing up template arguments with constructor arguments, which shows a more fundamental problem than not knowing the API for multimap… – abarnert Jun 28 '12 at 04:50
  • @abarnert: Maybe the example I added will help. – Vaughn Cato Jun 28 '12 at 04:55
  • I personally preffer using `MapType::value_type` instead of `make_pair` because you can _see_ to what map type the data belongs to. – PaperBirdMaster Jun 28 '12 at 06:13
2

std::pair<string,string>("000","000") should do it.

The code contained between < and > indicates the types of the variables you're inserting-- in this case strings

loki11
  • 374
  • 1
  • 4