[英]How do I check if one vector is a subset of another?
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. 目前,我认为我最好的选择是使用std :: set_intersection,然后检查较小输入的大小是否与set_intersection填充的元素数相同。
Is there a better solution? 有更好的解决方案吗?
Try this: 尝试这个:
if (std::includes(set_one.begin(), set_one.end(),
set_two.begin(), set_two.end()))
{
// ...
}
About includes() . 关于includes() 。
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).
includes()算法比较两个排序的序列,如果范围[start2,finish2]中的每个元素都包含在[start1,finish1]范围内,则返回true。 It returns false otherwise.
否则返回false。 includes() assumes that the sequences are sorted using operator<(), or using the predicate comp.
includes()假定使用operator <()或使用谓词comp对序列进行排序。
runs in 跑进来
At most ((finish1 - start1) + (finish2 - start2)) * 2 - 1 comparisons are performed.
最多((finish1 - start1)+(finish2 - start2))* 2 - 1执行比较。
Plus O(nlog(n)) for sorting vectors. 加O(nlog(n))用于排序向量。 You won't get it any faster than that.
你不会比这更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.