gpt4 book ai didi

c++ - 对无序集执行 set_difference

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:09 26 4
gpt4 key购买 nike

set_difference算法需要以下内容

The elements in the ranges shall already be ordered according to this same criterion

哈希表不是这种情况。

我正在考虑根据 std::remove_copy 实现集合差异 A-B,其中删除标准是集合 B 中存在 A 的元素。

是否有一种标准、有效、最快、最安全的方法来做到这一点?

最佳答案

如果您有两个哈希表,最有效的方法应该是遍历其中一个,在另一个哈希表中查找每个元素。然后将找不到的那些插入第三个容器中。粗略的草图可能如下所示:

std::vector<int> result;
std::copy_if(lhs.begin(), lhs.end(), std::back_inserter(result),
[&rhs] (int needle) { return rhs.find(needle) == rhs.end(); });

关于c++ - 对无序集执行 set_difference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22710331/

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