gpt4 book ai didi

c++ - 提示错误时的 emplace_hint 性能

转载 作者:可可西里 更新时间:2023-11-01 17:37:45 26 4
gpt4 key购买 nike

我正在尝试确定是否应该使用 emplace_hint 将 key 插入 multimap(与常规 emplace 相对)。我已经在较早的操作中(在同一个键上)计算了键的范围:

range = multimap.equal_range(key); 

我应该使用 range.firstrange.second 还是什么都不作为插入键值对的提示?如果范围为空怎么办?

最佳答案

Should I use range.first, range.second, or nothing as a hint to insert the key, value pair?

作为std::multimap::emplace_hint()状态:

Inserts a new element into the container as close as possible to the position just before hint.

(重点是我的)你应该使用范围内的 second 迭代器,它应该使插入更有效率:

Complexity

Logarithmic in the size of the container in general, but amortized constant if the new element is inserted just before hint.

至于空范围,使用 second 迭代器仍然没问题,因为它应该始终指向大于元素或在最后一个元素之后(如果不存在的话)。

关于c++ - 提示错误时的 emplace_hint 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com