C ++模板类中的equal_to用法

我对如何使用此方法感到困惑。我尝试了以下方法:

std::equal_to(T objA, T objB);

如果这样使用我会出错。但是,我看到了无数使用它的示例,例如:

pairs1 = mismatch(v1.begin(), v1.end(), 
                  v2.begin(), 
                  equal_to<int>()); 

应该如何使用此方法?

样本错误:

prog.cpp: In function ‘int main()’:
prog.cpp:25:31: error: no matching function for call to ‘std::equal_to<int>::equal_to(std::vector<int>&, std::vector<int>&)’
  pairs1 = equal_to<int>(v2, v1)); 
                               ^
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/random:40,
                 from /usr/include/c++/5/bits/stl_algo.h:66,
                 from /usr/include/c++/5/algorithm:62,
                 from prog.cpp:2:
/usr/include/c++/5/bits/stl_function.h:352:12: note: candidate: constexpr std::equal_to<int>::equal_to()
     struct equal_to : public binary_function<_Tp, _Tp, bool>
            ^
/usr/include/c++/5/bits/stl_function.h:352:12: note:   candidate expects 0 arguments, 2 provided
/usr/include/c++/5/bits/stl_function.h:352:12: note: candidate: constexpr std::equal_to<int>::equal_to(const std::equal_to<int>&)
/usr/include/c++/5/bits/stl_function.h:352:12: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/5/bits/stl_function.h:352:12: note: candidate: constexpr std::equal_to<int>::equal_to(std::equal_to<int>&&)
/usr/include/c++/5/bits/stl_function.h:352:12: note:   candidate expects 1 argument, 2 provided
评论
  • 妾随
    妾随 回复

    The template argument is the type of the arguments. You say that the arguments to the std::equal_to function are int, then you pass two vectors.

    让编译器自动推导类型,如

    bool_result = equal_to(v2, v1);
    

    或使用正确的类型:

    bool_result = equal_to<std::vector<int>>(v2, v1);