简体   繁体   English

带提示的std :: map :: insert_or_assign的复杂性

[英]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? 如果元素已经存在于地图中,则std::map::insertstd::map::assign的时间复杂度是多少? CPP-Reference says that it's the same as emplace_hint , which says: CPP-Reference表示与emplace_hint相同,后者表示:

"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 ? 提示应该是lower_bound还是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). 如果键已经存在,那么它甚至比这更简单,不需要将新节点添加到映射中,只需分配即可,因此再次摊销O(1)。 The words "just before hint" suggest that the hint should be slightly above the key mapped, ie an upper_bound . 单词“在提示之前”暗示提示应略高于映射的键,即upper_bound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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