如何检查一个向量是否是另一个向量的子集? [英] How do I check if one vector is a subset of another?
问题描述
目前,我认为我最好的选择是使用std :: set_intersection,然后检查较小的输入的大小是否与set_intersection填充的元素数量相同。
Currently, I think my best option is to use std::set_intersection, and then check if the size of the smaller input is the same as the number of elements filled by set_intersection.
有更好的解决方案吗?
推荐答案
尝试:
if (std::includes(set_one.begin(), set_one.end(),
set_two.begin(), set_two.end()))
{
// ...
}
href =http://stdcxx.apache.org/doc/stdlibref/includes.html =nofollow> includes()。
About includes().
includes()算法比较两个
排序序列,如果
范围内的每个元素[start2,
finish2)包含在范围
中,则返回true b [start1,finish1)。否则返回false
。 include()假定
序列是使用
运算符<()或使用谓词
comp来排序的。
The includes() algorithm compares two sorted sequences and returns true if every element in the range [start2, finish2) is contained in the range [start1, finish1). It returns false otherwise. includes() assumes that the sequences are sorted using operator<(), or using the predicate comp.
运行在
最多((finish1 - start1)+(finish2
- start2)执行2 - 1个比较。
At most ((finish1 - start1) + (finish2 - start2)) * 2 - 1 comparisons are performed.
Plus O(nlog(n))用于排序向量。你不会比这更快。
Plus O(nlog(n)) for sorting vectors. You won't get it any faster than that.
这篇关于如何检查一个向量是否是另一个向量的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!