简体   繁体   English

如何检查一个向量是否是另一个向量的子集?

[英]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.

相关问题 如何有效地将唯一对象从一个矢量复制到另一个矢量(由相同对象的子集组成)? - How do I efficiently copy unique objects from one vector to another (which is made up of a subset of identical objects)? 如何在c ++中检查向量是否是另一个向量的子集 - How to check whether a vector is a subset of another in c++ 如何将一个 std::vector 移动附加到另一个? - How do I move-append one std::vector to another? 无需复制即可将向量构建为另一个向量的子集 - Build a vector as a subset of another one without copying 优化检查位向量是否是另一个的适当子集? - Optimize check for a bit-vector being a proper subset of another? 我如何检查矢量 <bool> 实际上是位向量而不是字节的向量? - How do I check if vector<bool> is actually a vector of bits and not bytes? 确定一个向量是否是另一个向量的子集的有效方法? - Efficient way of determining whether one vector is a subset of another or not? 如何针对另一个向量对向量进行排序? - How do I sort a vector with respect to another vector? 如何将地图的内容从一个类别复制到另一个类别的新矢量 - How do I copy the contents of a map from one class into a new vector from another class 如何检查字符串是否是另一个字符串的正确子集 - How to check if string is a proper subset of another string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM