c++ - is_permutation 的编译器错误和结构的谓词

标签 c++ algorithm struct compiler-errors predicate

我不明白我在最后一行遇到的编译器错误:

#include <iostream>
#include <algorithm>


struct X{
  int number;
};
bool same_number_X(const int & a, const X & b)
{ return a == b.number; }


bool same_number_int(const int & a, const int & b)
{ return a == b; }


int main(){

   std::vector<int> vec1{1,2,3};
   std::vector<int> vec2{2,3,1};
   std::vector<X> vec3{{2},{3},{1}};

   std::cout << std::is_permutation(vec1.begin(), vec1.end(), vec2.begin(), same_number_int);
   std::cout << std::is_permutation(vec1.begin(), vec1.end(), vec3.begin(), same_number_X);
}

可以看到错误HERE 它说

In instantiation of 'typename std::iterator_traits<_InputIterator>::difference_type std::count_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<X*, std::vector<X> >; _Predicate = std::_Bind<bool (*(std::_Placeholder<1>, int))(const int&, const X&)>; typename std::iterator_traits<_InputIterator>::difference_type = long int]':
c++/4.7/bits/stl_algo.h:4367:37: required from 'bool std::is_permutation(_FIter1, _FIter1, _FIter2, _BinaryPredicate) [with _FIter1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _FIter2 = __gnu_cxx::__normal_iterator<X*, std::vector<X> >; _BinaryPredicate = bool (*)(const int&, const X&)]'
source.cpp:23:90: required from here
c++/4.7/bits/stl_algo.h:4681:2: error: no match for call to '(std::_Bind<bool (*(std::_Placeholder<1>, int))(const int&, const X&)>) (X&)'

任何人都可以用非编译器语言解释/解决它吗? :-)

最佳答案

来自 C++11 标准的第 25.2.12/1 段:

template<class ForwardIterator1, class ForwardIterator2>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
                    ForwardIterator2 first2);

template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
                    ForwardIterator2 first2, BinaryPredicate pred);

1 Requires: ForwardIterator1 and ForwardIterator2 shall have the same value type. The comparison function shall be an equivalence relation.

在您上次调用 is_permutation() 时,第一个参数(从中推导出 ForwardIterator1 类型)是 int 值的迭代器,而第三个参数(从中推导出 ForwardIterator2 类型)是 X 值的迭代器。因此,您违反了上一段所述的先决条件。

关于c++ - is_permutation 的编译器错误和结构的谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209006/

相关文章:

c++ - 无法在 gcc 中创建 std::pair 的 const 成员

c++ - 在 Visual C++ 2003 中删除调试符号

c++ - 无法在 C++ 中访问数组的元素

asynchronous - 为什么在结果上使用匹配语句会出现 "expected type Future"错误?

python - 无法打开文件 'boost_python36-vc141-mt-gd-x64-1_68.lib'

algorithm - 棋盘边缘的移动更少

c# - 这个C#财务计算的 float 问题在哪里?

R循环和data.frame

c - 结构体中指针的偏移量,以及如何获取汇编中的值

c - 具有必须在 C 中的 .h 文件中公开的属性的私有(private)结构