简体   繁体   English

如何在c ++中检查向量是否是另一个向量的子集

[英]How to check whether a vector is a subset of another in c++

I am trying to find a simple way to check whether a vector is a subset of another without sorting the order of elements in the vector. 我试图找到一种简单的方法来检查向量是否是另一个的子集而不排序向量中的元素的顺序。 Both the vectors contain random number elements in them. 两个向量都包含随机数元素。

std::includes seems to work only for sorted ranges. std::includes似乎只适用于排序范围。 How can I accomplish this? 我怎么能做到这一点?

Copy the vectors. 复制向量。 Sort the copies. 对副本进行排序。 Then use std::includes on the copies. 然后在副本上使用std::includes

template <typename T>
bool IsSubset(std::vector<T> A, std::vector<T> B)
{
    std::sort(A.begin(), A.end());
    std::sort(B.begin(), B.end());
    return std::includes(A.begin(), A.end(), B.begin(), B.end());
}

My answer assumes that when you say "subset", you are really searching more for the equivalent of a "substring"; 我的回答是假设当你说“子集”时,你真的在​​寻找相当于“子串”的东西; that is, maintaining order of elements during the search. 也就是说,在搜索过程中维护元素的顺序。


Ultimately, I can't see how you could do this in anything less than O(n*m) . 最终,我无法看到你如何在低于O(n*m)任何事情中做到这一点。 Given that, you can just roll your own pretty simply: 鉴于此,您可以简单地自己动手:

template <typename T1, typename T2>
bool contains(std::vector<T1> const& a, std::vector<T2> const& b) {
   for (typename std::vector<T1>::const_iterator i = a.begin(), y = a.end(); i != y; ++i) {
      bool match = true;

      typename std::vector<T1>::const_iterator ii = i;
      for (typename std::vector<T2>::const_iterator j = b.begin(), z = b.end(); j != z; ++j) {
          if (ii == a.end() || *j != *ii) {
              match = false;
              break;
          }
          ii++;
      }

      if (match)
         return true;
   }

   return false;
}

Live demo. 现场演示。

(You could probably be more creative with the template parameters.) (你可能会对模板参数更有创意。)

This is assuming duplicates do NOT matter. 这是假设重复无关紧要。 So if you have two instances of the number 99 in vector a, then as long as vector b has at least one instance of the number 99, then it will be declared as a subset. 因此,如果在向量a中有两个数字99的实例,那么只要向量b至少有一个数字99的实例,那么它将被声明为子集。

bool isSubset(const std::vector<int>& a, const std::vector<int>& b)
{
    for (std::vector<int>::const_iterator i = a.begin(); i != a.end(); i++)
    {
        bool found = false;

        for (std::vector<int>::const_iterator j = b.begin(); j != b.end(); j++)
        {
            if (*i == *j)
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            return false;
        }
    }

    return true;
}

With no sorting... 没有排序......

template <typename T>
bool isSubsetOrEqual(std::vector<T> const& a, std::vector<T> const& b) {
   for(auto const& av:a){
      if(std::find(b.begin(),b.end(),av)==b.end())
          return false;
   }
   return true;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM