如何在向量内找到向量

时间:2018-11-17 13:12:12

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

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

#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; }
                   ^

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

1 个答案:

答案 0 :(得分:1)

在您的代码中,“您正在int s的向量中寻找B s”。

编辑:根据您的评论,我了解您要使用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;
}

输出:

  

在向量中找到的元素