【问题标题】:How to find a vector inside a vector如何在向量中找到向量
【发布时间】:2018-11-17 13:12:11
【问题描述】:

我正在尝试编写一个程序,该程序必须在向量中搜索向量的存在:

#include <iostream>
#include <algorithm>    
#include <vector>  
#include <string>

struct B
{
    std::vector<int> a;
};

struct A
{
    std::vector<B> a;
    int x;
    std::string n;
};

int main()
{
    A s;
    B b1,b2, b3;
    b1.a.push_back(10);
    b1.a.push_back(29);
    b2.a.push_back(50);
    b2.a.push_back(69);
    s.a.push_back(b1);
    s.a.push_back(b2);
    std::vector<int> a22;
    b3.a.push_back(10);
    b3.a.push_back(29);
    auto it = std::search(s.a.begin(), s.a.end(), b3.a.begin(), b3.a.end());
    if (it != s.a.end())
        std::cout << "Element found in vector\n";
    else
       std::cout << "Element not found in vector\n";
   return 0;
}

但是当我编译代码时,我得到了很多错误:

$ c++ -std=c++11 try66.cpp
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:71:0,
                 from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/char_traits.h:39,
                 from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ios:40,
                 from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ostream:38,
                 from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/iostream:39,
                 from try66.cpp:1:
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equal_to_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<B*, std::vector<B> >; _Iterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]':
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algo.h:237:37:   required from '_ForwardIterator1 std::__search(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _BinaryPredicate) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<B*, std::vector<B> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _BinaryPredicate = __gnu_cxx::__ops::_Iter_equal_to_iter]'
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algo.h:4023:47:   required from '_FIter1 std::search(_FIter1, _FIter1, _FIter2, _FIter2) [with _FIter1 = __gnu_cxx::__normal_iterator<B*, std::vector<B> >; _FIter2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
try66.cpp:31:71:   required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/predefined_ops.h:86:23: error: no match for 'operator==' (operand types are 'B' and 'int')
       { return *__it1 == *__it2; }
                   ^

我不确定我们需要创建什么帮助函数来解决问题?

【问题讨论】:

  • 你在Bs的向量中寻找ints
  • 谢谢 - 我如何从具有 B 的 int 向量的 A 的结构向量 'a' 中搜索 int 向量?

标签: c++ c++11 search vector


【解决方案1】:

在您的代码中,“您正在Bs 的向量中寻找ints”。

编辑:根据您的 cmets,我了解到您想使用 std::find_first_of,而不是搜索方法。在Difference between std::search and std::find_first_of 中阅读更多内容。

您必须向std::search 提供自己的谓词,以帮助进行比较,例如:

#include <iostream>
#include <algorithm>    
#include <vector>  
#include <string>

struct B
{
    std::vector<int> a;
};

struct A
{
    std::vector<B> a;
    int x;
    std::string n;
};

bool mypredicate (B b, int value) {
    return std::find(b.a.begin(), b.a.end(), value) != b.a.end();
}


int main()
{
    A s;
    B b1,b2, b3;
    b1.a.push_back(10);
    b1.a.push_back(29);
    b2.a.push_back(50);
    b2.a.push_back(69);
    s.a.push_back(b1);
    s.a.push_back(b2);
    std::vector<int> a22;
    b3.a.push_back(10);
    b3.a.push_back(29);
    auto it = std::find_first_of(s.a.begin(), s.a.end(), b3.a.begin(), b3.a.end(), mypredicate);
    if (it != s.a.end())
        std::cout << "Element found in vector\n";
    else
       std::cout << "Element not found in vector\n";
   return 0;
}

输出:

向量中的元素

【讨论】:

  • 谢谢,我明白了,但代码的输出是 Element not found in vector - mypredicate 是否需要添加一些额外的验证?
  • 您可以简化 mypredicate 以返回 std::find(b.a.begin(), b.a.end(), value) != b.a.end();
  • 谢谢 - 但即使我们确保搜索向量存在,搜索向量也总是失败
  • 似乎 mypredicate 被调用三次,值为 10,29 和 10,然后失败
  • 感谢您提供的详细信息和示例 - 它让我深入了解如何实施和学习新知识
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多