C++20 sections · 1024 units
Open in Course

Inserting Elements

How to add entries

Use m[key] = value to insert or update an entry. If the key already exists, this overwrites the old value. If it doesn't exist, a new entry is created automatically. You can also use m.insert({key, value}) which only inserts if the key doesn't already exist.

It returns a pair with an iterator and a bool telling you whether insertion happened. For better performance with complex types, use m.emplace(key, value) which constructs the pair directly in place.

All three methods take O(log⁡n)O(\log n) time due to the tree structure.