newprint
newprint

Reputation: 7136

Insertion into pair that is mapped value in multimap in C++

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 !

Upvotes: 0

Views: 172

Answers (2)

Vaughn Cato
Vaughn Cato

Reputation: 64308

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")));

Upvotes: 5

loki11
loki11

Reputation: 374

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

Upvotes: 2

Related Questions