c++ - 在另一个 vector 中搜索一个 vector

标签 c++ search stl stdvector

我正在尝试查看 vector v1 是否在 vector v2 内部。

例如,如果 v1= (b, a) 且 v2 = (g, e, f, a, b)。我需要检查 v2 中的 b 和 a 现在。

仅当顺序相同时,以下代码才会对我有帮助。

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

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

目前我正在通过以下方式实现

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"; 
}

有没有办法使用 std::search 来实现上述功能?

最佳答案

您可以使用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

请注意,在使用 std::set_intersection 之前,需要使用相同的排序函数对两个 vector 进行排序。因为它依赖于使用 operator< 比较元素

此外,您可以使用 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';
}

输出:

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 (上面的例子直接来自引用)

任何一个都可以完成这项工作,具体取决于您想要做什么。

关于c++ - 在另一个 vector 中搜索一个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42560182/

相关文章:

c++ - 如何在 VC++ 项目中强制执行附加包含文件搜索顺序?

c++ - 将 char 类型转换为 int 以用作 vector C++ 的索引

c++ - 反向迭代 vector

c++ - ifstream 如何使用 C++ 从特定行开始读取行

c++ - 错误预期为 ';'

elasticsearch - Elasticsearch相关性-具有相似名称的文档

arrays - 如何有效地搜索数组中的特定序列?

c# - 从字符串中提取所有出现的特定字符

c++ - 访问自定义 2-dim C++ 容器元素的最佳方法

c++ - 使用自定义比较器设置的 STL 允许重复元素