What’s the difference between those two versions of insertion into a c++ map :
map<string,double> myMap;
// version 1
myMap["david"] = 123.123;
// version 2
myMap.insert(std::make_pair("david" ,123.123));
Regards
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first one will update the value if the key already exists, but the second one will not update it if the key already exists.
The
insertfunction returnsstd::pair<iterator,bool>. The boolean value of the pair tells you whether the insertion was successful or not.Now read the documentation for more detail:
std::map::insertstd::map::operator[]