简体   繁体   中英

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 !

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM