Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

iirc, there are 5 ways to put something into a std::map

  operator[], insert(), emplace(), try_emplace(), insert_or_assign()
And 2 of them don't overwrite an existing value.

Lots of people are surprised that insert() can fail. And even more surprised that a RHS [] inserts a default value. I'm not a fan of APIs that surprise.





> I'm not a fan of APIs that surprise.

It really depends on what you got used to. C++ was the first language, which, I would say, learned. I’m still surprised to day that traversing a set is not in order by default.

In this specific case, emplace should be your default option, and you should really know why that’s the case, and why you have this many options.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: