提问人:sasquires 提问时间:5/20/2023 更新时间:5/20/2023 访问量:131
我们可以通过提供提示来优化 'std::map::find' 的性能吗?
Can we optimize the performance of `std::map::find` by providing a hint?
问:
该方法允许我们通过提供一个“提示”迭代器来优化性能,我们希望该迭代器非常接近放置项目的位置。当您快速连续放置多个项目时,这可能是最有用的。std::map::emplace_hint
std::map::emplace
我正在编写一些代码,我希望需要调用这些代码才能快速连续地获取大量值。我希望钥匙彼此靠近,但不一定是相邻的。没有 ,但是有没有办法使用标准库中的现有方法做类似的事情?基本上,我只想通过提供一个靠近键的迭代器来加快速度。find
std::map::find_hint
find
当然,这个问题可能更广泛地适用于大多数排序的 C++ 标准库容器,但今天我特别感兴趣。std::map
答:
0赞
273K
5/20/2023
#1
不,我们不能。这是完整的 std::map
手册。
如果您非常确定要查找的下一个键可以在迭代器后续步骤中到达,则可以使用算法。std::find_if()
log_2(map.size())
#include <algorithm>
#include <iostream>
#include <map>
int main() {
std::map<int, int> m{{1, 1}, {2, 2}};
auto it = m.find(1);
if (it != m.end())
std::cout << it->second << " ";
int k = 2; // next key to find
it = std::find_if(it, m.end(),
[k](const auto& v) { return v.first == k; });
if (it != m.end())
std::cout << it->second << "\n";
}
// Output: 1 2
评论
0赞
sasquires
5/20/2023
是的,我看过手册,但正在寻找其他想法。会考虑使用并回复您。std::find
0赞
sasquires
5/20/2023
看起来没有任何超载可以直接做我想要的事情。特别是,我可以尝试向前搜索一定数量的步骤,然后向后搜索一定数量的步骤,但正如 Yksisarvinen 建议的那样,使用迭代器手动搜索可能比使用 .std::find
std::find
上一个:使用成员函数启动线程
评论
unordered_map
find
O(1)
map
find
O(log(N))
)find
log(n)
unordered_map
map
unordered_map
unordered_map
map