Emplace Enhancements for Maps and Unordered Maps
Here we'll work with some of the new methods available in maps.
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 likeemplace()
. -
emplace()
might move from the input parameter when the key is in the map, that’s why it’s best to usefind()
before such emplacement. -
insert_or_assign()
- gives more information than operator[]
- as it returns if the element was newly inserted or updated and also works with types that have no default constructor.
try_emplace
Method
Here’s an example:
Get hands-on with 1400+ tech skills courses.