【问题标题】:Check if unordered_set contains all elements in other unordered_set - C++检查 unordered_set 是否包含其他 unordered_set 中的所有元素 - C++
【发布时间】:2018-06-26 05:34:57
【问题描述】:

我是 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;
}

【问题讨论】:

  • 一一检查每个元素? (可能在比较尺寸以排除明显的“否”情况后)
  • 感谢 Caleth/Mark。您的提示使我编写了上述方法。我认为这是我能做的最好的吗?
  • 我不是 100% 确定,但这似乎是最好的方法。复杂性是线性的,因为 find 是 const time。
  • 没有一个神奇的按钮可以按下并立即得到答案,无论一个 unordered_set 是否包含另一个 unordered_set 的所有值。手册,一个一个,搜索是唯一的方法;禁止按小值集的顺序进行特殊用例,在这种情况下,可以使用位掩码等来完成一些愚蠢的事情......此外,这似乎比基于排序的更快,这似乎是O(n log n),而手册查找似乎是O(n)。仅仅因为通过少量代码完成某事并不一定意味着它“更快”。

标签: c++ unordered-set


【解决方案1】:

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


这是一些演示代码,带有测试:

#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() 进行测试)


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

【讨论】:

    【解决方案2】:

    免责声明:这不是最有效的方法。这是一种解决方案的尝试,它与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;
    }
    

    Small live-example

    那么includes_unordered 可以像std::includes 一样使用。

    【讨论】:

    • 两个范围都需要排序才能与 std::includes 一起使用
    • 这不起作用,因为必须对范围进行排序。 unordered_set 未排序。
    • 当然你们是对的人,我画得很快。我已经更新了答案。
    • 这似乎是一个 O(m_×_n) 算法,其中 mn 是两组。虽然它会起作用,但它远没有效率。
    • @TobySpeight 正确,您的回答更有效率。这只是我对与std::includes 一样通用和灵活的东西的看法。我添加了一个简短的免责声明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多