Search⌘ K
AI Features

Emplace Enhancements for Maps and Unordered Maps

Understand how C++17 introduces try_emplace and insert_or_assign methods to improve handling of maps and unordered maps. Learn when to use these methods to avoid unnecessary moves and support non-default constructible types, enhancing your ability to manage map elements efficiently.

We'll cover the following...

​With C++17 you get two new methods for maps and unordered maps:

  • try_emplace() - if the object already exists, then it does nothing; otherwise,​ it behaves like emplace().

  • emplace() might move from the input parameter when the key is in the map, that’s why it’s best to use find() before such emplacement.

  • insert_or_assign() - ...