c++ - 如何检查 unordered_set 是否重叠?

标签 c++ algorithm optimization unordered-set

我有两个 unordered_sets,需要检查第一个集合的所有元素是否也是第二个集合的元素。

有没有快速的方法来做到这一点,或者我应该使用另一个容器?<​​/p>

最佳答案

只需使用循环(或相应的算法)。复杂度与要测试的范围的大小(大约)呈线性关系。

template <typename UnorderedSet, typename Iterator>
bool contains_all(UnorderedSet&& set, Iterator first, Iterator last)
{
    using value_type = std::iterator_traits<Iterator>>::value_type;
    return std::all_of(first, last, [&set] (const value_type& value) {
        return set.count(value);
    });
}

关于c++ - 如何检查 unordered_set 是否重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22206792/

相关文章:

c++ - 如何在同一位置的所有 Linux 发行版中找到包含 CPU 温度和风扇速度信息的文件?

algorithm - 销售排名算法

algorithm - MATLAB 中数字的负数

java - 将对象列表中的双列表对象转换为字符串

c++ - C++ lambda 中的静态变量

c++ - Microsoft 的 C 库函数的 C++ 安全版本如何知道静态缓冲区的大小?

r - R 中的最小二乘优化

python - 这个算法优化了吗?不然还能怎么用呢?

c++ - 在 C++ 中运行时在用户定义的类之间进行更改

algorithm - 贪心算法按顺序查找潜在的加权事件?