【发布时间】:2021-05-23 08:46:48
【问题描述】:
#include <set>
#include <vector>
using Value = std::vector<int>;
int main()
{
auto coll = std::set<Value>{};
// ......
// Insert many values here.
// ......
auto new_value = Value{1, 2, 3};
auto const pos = coll.find(new_value);
if (pos != coll.end())
{
// Good
coll.emplace_hint(coll.erase(pos), std::move(new_value));
}
else
{
// Performance penalty!
// No hint here, though coll.find(new_value) knows that.
coll.emplace(std::move(new_value));
}
}
为什么std::set::find不提供提示迭代器?
【问题讨论】:
-
我不确定该迭代器的外观。您或许可以改用
std::set::lower_bound。
标签: c++ performance c++11 c++17 standards