简体   繁体   中英

Emplace and insert in unordered_map in Visual Studio?

I'm struggling to do the most basic thing with an unordered_map in c++ using VS2010: inserting something.

unordered_map<string, string> cats;
//cats.insert("Adam", "Streak");
//cats.emplace("Kyra", "Shadow");

I'm sure I'm making a very simple mistake, but I have spent two hours now trying to figure it out to to avail. Both the commented lines above give errors when uncommented.

When cats.insert("Adam", "Streak"); is uncommented, the error is:

Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\xfunctional

When cats.emplace("Kyra", "Shadow"); is uncommented, the error is:

Error 1 error C2780: 'std::pair<_Ty1,_Ty2> std::_Hash<_Traits>::emplace(_Valty &&)' : expects 1 arguments - 2 provided

I am quite confused, as there are several examples across the internet using this exact syntax. For example, cplusplus.com here .

What am I doing wrong?

Both functions accept std::pair<A,B> make_pair is the easiest way to make a pair.

cats.insert(std::make_pair("Adam", "Streak"));
cats.emplace(std::make_pair("Kyra", "Shadow"));

or

cats.insert(std::pair<std::string, std::string>("Adam", "Streak"));

http://ideone.com/43GqyB

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