1

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

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

你可以在这里看到错误 它说

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&)'

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

4

1 回答 1

3

来自 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要求ForwardIterator1并且ForwardIterator2 应具有相同的值类型。比较函数应为等价关系。

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

于 2013-03-04T19:03:33.177 回答