检查unordered_set是否包含其他unordered_set中的所有元素 - C++
Check if unordered_set contains all elements in other unordered_set - C++
我是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::vector
或std::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
一样使用。
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 如何使用set实现无序数据结构?
- 使用运算符调用 void 函数时出错<set>
- 修改"std::set"中用户定义类型的值
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- 如何使用 lower_bound/upper_bound 从 std::set 获取索引号?
- 如何在 C++ 中转发声明 std::set?
- 重构使用动态强制转换的 std::set 的比较运算符
- set::find 查找不存在的元素
- 为什么 std::set.erase(first, last) 会影响从中获取 (first, last) 的容器?
- 将 std:set<int32_t> 复制到 std::set <uint32_t>的好方法
- 错误 C2676:std::set::const_iterator 没有运算符 + 函数?
- std::set 是否将对象连续存储在内存中?
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- google test PrintTo for std::set<std::string>