【问题标题】:Why does std::set::find not provide a hint iterator?为什么 std::set::find 不提供提示迭代器?
【发布时间】: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


【解决方案1】:

std::set::lower_bound() 的结果可用作emplace_hint() 的提示。所以只需使用lower_bound() 而不是find(),并检查lower_bound() 返回的键是否与您要查找的匹配,而不是检查find() 返回的迭代器是否为end()

【讨论】:

    【解决方案2】:

    在考虑迭代器的 == 运算符的传递性和对称性时,这会很麻烦。

    i1i2 成为通过find 检索到的针对set 中不存在的不同对象的提示。

    我们有

    • i1 == set.end()
    • set.end() == i2

    因此i1 == i2,所以我们希望这种提示在插入时具有相同的行为,但您描述的所需行为是不同的行为(在传递通过@检索的迭代器时提高emplace的性能987654330@ 与您放置的值相同)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2017-08-15
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多