简体   繁体   English

搜索另一个矢量内的矢量

[英]Searching a vector inside another vector

I am trying to see if vector v1 is inside vector v2. 我试图看看矢量v1是否在矢量v2内。

For example, if v1= (b, a) and v2 = (g, e, f, a, b). 例如,如果v1 =(b,a)并且v2 =(g,e,f,a,b)。 I need to check both b and a present in v2. 我需要在v2中检查b和a。

I following code will help me only if order is same. 我下面的代码只有在订单相同时才会帮助我。

std::search(v2.begin(), v2.end(), v1.begin(), v1.end());

ie, if v2 = (g, e, f, b, a) 即,如果v2 =(g,e,f,b,a)

Currently I am achieving through following way 目前我通过以下方式实现

for (std::vector<std::string>::iterator it = v1.begin(); it != v1.end(); ++it)
{
    if (std::find(v2.begin(), v2.end(), *it) != v2.end())
        std::cout << "found\n";
    else
        std::cout << "not found\n"; 
}

Is there a way to achieve using the above using std::search? 有没有办法使用std :: search实现上述使用?

You could use std::set_intersection : 你可以使用std::set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
    std::vector<char> v1{'a','b','e','f','g'};
    std::vector<char> v2{'a','b'};
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());

    std::vector<char> v_intersection;

    std::set_intersection(v1.begin(), v1.end(),
                          v2.begin(), v2.end(),
                          std::back_inserter(v_intersection));
    for(int n : v_intersection)
        std::cout << n << ' ';
}

See the reference 参见参考文献

Please note that it is required that the two vectors are sorted using the same sort function prior to using std::set_intersection because it relies on comparing elements using operator< 请注意,在使用std::set_intersection之前,需要使用相同的排序函数对两个向量进行排序,因为它依赖于使用operator<比较元素

Additionally you could use std::includes 另外你可以使用std::includes

#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>

int main()
{
  std::vector<char> v1 {'a', 'b', 'c', 'f', 'h', 'x'};
  std::vector<char> v2 {'a', 'b', 'c'};
  std::vector<char> v3 {'a', 'c'};
  std::vector<char> v4 {'g'};
  std::vector<char> v5 {'a', 'c', 'g'};

  for (auto i : v1) std::cout << i << ' ';
  std::cout << "\nincludes:\n" << std::boolalpha;

  for (auto i : v2) std::cout << i << ' ';
  std::cout << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n';
  for (auto i : v3) std::cout << i << ' ';
  std::cout << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n';
  for (auto i : v4) std::cout << i << ' ';
  std::cout << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n';
  for (auto i : v5) std::cout << i << ' ';
  std::cout << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n';

  auto cmp_nocase = [](char a, char b) {
    return std::tolower(a) < std::tolower(b);
  };

  std::vector<char> v6 {'A', 'B', 'C'};
  for (auto i : v6) std::cout << i << ' ';
  std::cout << ": (case-insensitive) "
            << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase)
            << '\n';
}

OUTPUT: OUTPUT:

a b c f h x
includes:
a b c : true
a c : true
g : false
a c g : false
A B C : (case-insensitive) true

Here is the reference page (the example above is direct from the reference) 这是参考页面 (上面的例子直接来自参考)

Either one could do the job depending on what you are trying to do. 任何人都可以根据你想要做的事情来完成这项工作。

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

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