简体   繁体   中英

Complexity of std::map::insert_or_assign with hint

What is the time complexity of std::map::insert or std::map::assign with hint provided in the case that the element already exists in the map? CPP-Reference says that it's the same as emplace_hint , which says:

"amortized constant if the new element is inserted just before hint"

But:

  1. What if no new element is inserted?
  2. And should the hint be lower_bound or upper_bound ?

If the key already exists, then it is even simpler than that, no need to add new node into the map, just an assignment, so again, ammortized O(1). The words "just before hint" suggest that the hint should be slightly above the key mapped, ie an upper_bound .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM