简体   繁体   English

是否所有x元素都存在于y中(排序的向量)?

[英]Are all elements of `x` present in `y` (sorted vectors)?

Let x and y be two sorted vectors. xy为两个排序的向量。 I would like to figure out which of the three is correct 我想找出三个中的哪一个是正确的

A. All elements of `y` are present in `x`
B. Some but not all elements of `y` are present in `x`
C. No element of `y` is present in `x`
U. Undefined (because `y` is empty)

A naive way to achieve this is with 天真的方法是

template<typename T>
char f(std::vector<T> x, std::vector<T> y)
{
    if (y.size() == 0)
    {
        return 'U';
    }

    bool whereAnyElementFound = false;
    bool whereAnyElementNotFound = false;
    for (T& y_element : y)
    {
        bool isElementFound = std::find(x.begin(),x.end(),y_element) != x.end();
        if (isElementFound)
        {
            whereAnyElementFound = true;
            if (whereAnyElementNotFound)
            {
                return 'B';
            }
        } else
        {
            whereAnyElementNotFound = true;
            if (whereAnyElementFound)
            {
                return 'B';
            }
        }
    }
    if (whereAnyElementFound)
    {
        return 'A';
    } else if (whereAnyElementNotFound)
    {
        return 'C';
    }
    abort();
}

The function correctly match the following inputs to outputs 该功能将以下输入正确匹配到输出

inputs: x = {1,2,4,5} y = {2,5}
output: A

inputs: x = {1,2,4,5} y = {2,7}
output: B

inputs: x = {1,2,4,5} y = {6,7}
output: C

inputs: x = {1,2,4,5} y = {}
output: U

However, this method does not take advantage of the fact that both vectors are sorted. 但是,该方法没有利用两个向量都已排序的事实。 How can this function made faster for larger vectors? 对于较大的向量,如何使此函数更快?

For the cost of O(N) additional space you can use std::set_intersection . 对于O(N)额外空间,您可以使用std::set_intersection It has O(2(N1+N2-1)) complexity and generates a "set" of all the common elements between the two vectors. 它具有O(2(N1+N2-1))复杂度,并生成两个向量之间所有公共元素的“集合”。 You can then check that new "set"'s size to figure out A, B, C and U. 然后,您可以检查新“集合”的大小以找出A,B,C和U。

int main()
{
    std::vector<int> v1{1,2,3,4,5,6,7,8};
    std::vector<int> v2{5,7,9,10};

    std::vector<int> intersection;

    std::set_intersection(v1.begin(), v1.end(),
                          v2.begin(), v2.end(),
                          std::back_inserter(intersection));
    if (intersection.size() == v2.size() && v2.size() > 0)
        std::cout << "A";
    else if (intersection.size() > 0)
        std::cout << "B";
    else if (intersection.size() == 0 && v2.size() > 0)
        std::cout << "C";
    else
        std::cout << "U";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:如何比较多个向量,然后创建一个包含所有向量的所有元素的新排序向量 - C++: how to compare several vectors, then make a new sorted vector that contains ALL elements of all vectors 如何结合两个排序的向量并组合重叠元素? - How to union two sorted vectors and combine overlapping elements? 为什么std :: nth_element返回N &lt;33个元素的输入向量的排序向量? - Why does std::nth_element return sorted vectors for input vectors with N < 33 elements? 迭代std :: map <X,std::vector<Y> &gt;并对矢量进行排序 - Iterating over std::map<X,std::vector<Y> > and sorting the vectors 如何从位置和地标获取(X,Y)向量 - How to get (X,Y) vectors from position and landmark 生成某些形式的两个值(x,y)的矢量的函数 - Function to generate some vectors of two values (x,y) of some forms 在向量向量中查找值的 x,y 索引 - C++ - Find the x,y index of a value in a vector of vectors - C++ std 将向量 float[3] 移动到向量 class x,y,z - std move vectors float[3] into vector class x,y,z C ++如何将已排序的向量合并到一个已排序的向量/弹出所有这些向量中的最小元素? - C++ How to merge sorted vectors into a sorted vector / pop the least element from all of them? 收集唯一的排序向量 - Collecting unique sorted vectors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM