简体   繁体   English

如何检查unordered_set是否重叠?

[英]How to check unordered_set for overlaping?

I have two unordered_sets and need to check if all the elements of the first one are also elements of the second one. 我有两个unordered_sets,需要检查第一个的所有元素是否也是第二个的元素。

Is there a fast way to do this or should I use another container? 有没有一种快速的方法来执行此操作,还是应该使用另一个容器?

Just use a loop (or a corresponding algorithm). 只需使用循环(或相应的算法)即可。 The complexity is (approximately) linear in the size of the range to be tested. 在要测试的范围大小上,复杂度是(近似)线性的。

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);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM