检查unordered_set是否包含其他unordered_set中的所有元素 - C++

Check if unordered_set contains all elements in other unordered_set - C++

本文关键字:set unordered 元素 C++ 其他 是否 包含 检查      更新时间:2023-10-16

我是C++新手,被要求将Java程序转换为C++。我正在尝试编写一种方法来检查unordered_set中的所有元素是否存在于另一个unordered_set中。我发现下面的示例使用 hash_set 但hash_set已被弃用,建议现在使用 unordered_set。

// returns true if one contains all elements in two
bool SpecSet::containsAll(hash_set<Species*> one, hash_set<Species*> two) {
   sort(one.begin(), one.end());
   sort(two.begin(), two.end());
   return includes(one.begin(), one.end(), two.begin(), two.end());
}

所以我需要一种方法来使用unordered_set来做到这一点。排序不适用于无序集,查找速度很重要,所以我不想使用有序集。

bool SpecSet::containsAll(unordered_set<Species*> one, unordered_set<Species*> two) {
   return ?;
}

我真的很感激一些关于有效做到这一点的方法的帮助。

编辑:我想这会起作用。似乎没有比这更有效的方法了,只能一分为二地循环。

bool SpecSet::containsAll(unordered_set<Species*> one, unordered_set<Species*> two) {
   if(two.size() > one.size())
   {
      return false;
   }
   for(Species *species : two)
   {
      if(one.find(species) == one.end())
      {
         return false;
      }
   }
   return true;
}

对于未排序的集合,没有比迭代较小的集合更快的算法了,同时测试每个元素是否是较大集合的成员。 这将自然地扩展为 O(n(,其中 n 是假定子集的大小,因为我们执行 O(1( 查找操作 n 次。


下面是一些带有测试的演示代码:

#include <unordered_set>
template <typename T>
bool is_subset_of(const std::unordered_set<T>& a, const std::unordered_set<T>& b)
{
    // return true if all members of a are also in b
    if (a.size() > b.size())
        return false;
    auto const not_found = b.end();
    for (auto const& element: a)
        if (b.find(element) == not_found)
            return false;
    return true;
}
int main()
{
    const std::unordered_set<int> empty{ };
    const std::unordered_set<int> small{ 1, 2, 3 };
    const std::unordered_set<int> large{ 0, 1, 2, 3, 4 };
    const std::unordered_set<int> other{ 0, 1, 2, 3, 9 };
    return 0
        +  is_subset_of(small, empty) // small ⊄ ∅
        + !is_subset_of(empty, small) // ∅ ⊂ small
        +  is_subset_of(large, small) // large ⊄ small
        + !is_subset_of(small, large) // small ⊂ large
        +  is_subset_of(large, other) // large ⊄ other
        +  is_subset_of(other, large) // other ⊄ large
        + !is_subset_of(empty, empty) // ∅ ⊂ ∅
        + !is_subset_of(large, large) // x ⊂ x, ∀x
        ;
}

一个等效的,使用标准算法而不是编写显式循环:

#include <algorithm>
#include <unordered_set>
template <typename T>
bool is_subset_of(const std::unordered_set<T>& a, const std::unordered_set<T>& b)
{
    // return true if all members of a are also in b
    auto const is_in_b = [&b](auto const& x){ return b.find(x) != b.end(); };
    return a.size() <= b.size() && std::all_of(a.begin(), a.end(), is_in_b);
}

(显然使用相同的main()进行测试(


请注意,我们按引用而不是值传递集合,因为您已经指出这些集合太大,无法复制和排序它们。

免责声明:这不是最有效的方法。这是一种解决方案的尝试,该解决方案将像std::includes一样通用和灵活,同时支持无序迭代器范围。它不限于std::unordered_set,应该适用于任何其他容器,例如std::vectorstd::list


正如有人指出的std::includes需要对输入范围进行排序。目前,标准库中不支持无序范围。

查看可能的实现std::includes可以实现无序范围的版本。例如:

template<class InputIt1, class InputIt2>
bool includes_unordered(
    InputIt1 first1, InputIt1 last1,
    InputIt2 first2, InputIt2 last2)
{
    for (; first2 != last2; ++first2)
    {
        InputIt1 it1;
        for (it1 = first1; it1 != last1; ++it1)
        {
            if(*first2 == *it1)
                break;
        }
        if (it1 == last1)
            return false;
    }
    return true;
}

注意:容器的大小比较优化不是为了支持非唯一对象的容器。但是如果需要,可以使用 std::distance .

这是一个采用等价运算符的版本:

template<class InputIt1, class InputIt2, class Equivalence>
bool includes_unordered(
    InputIt1 first1, InputIt1 last1,
    InputIt2 first2, InputIt2 last2,
    Equivalence equiv)
{
    for (; first2 != last2; ++first2)
    {
        InputIt1 it1;
        for (it1 = first1; it1 != last1; ++it1)
        {
            if(equiv(*first2, *it1))
                break;
        }
        if (it1 == last1)
            return false;
    }
    return true;
}

小型活体示例

然后includes_unordered可以像std::includes一样使用。