4

如果我这样定义我的compare函数:

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

g++ 会抱怨:

g++ -Wall main.cpp -o main
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/algorithm:63:0,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Tp = Student, _Compare = bool (*)(Student&, Student&)]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2261:78:   instantiated from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2302:62:   instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Size = long int, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:5250:4:   instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
main.cpp:38:51:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2229:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2232:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’

Compilation exited abnormally with code 1 at Mon May 28 08:05:35

但是如果我定义了与const类型的比较,它将编译并正常工作。

这是所有代码:

class Student {
public:
    Student(string);
    string n;

};

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

Student::Student(string name) { n = name; }

int main()
{
    Student A = Student("A");
    Student B = Student("B");

    vector<Student> students;
    students.push_back(B);
    students.push_back(A);

    sort(students.begin(), students.end(), compare);

    cout << "After sort" << endl;
    for(vector<Student>::iterator i = students.begin(); i != students.end(); ++i) {
        cout << "Student: " << i->n << endl;
    }

    return 0;
}
4

3 回答 3

6

在这个实现中,std::sort使用

 const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare);

在您的情况下,_Tp是学生,并且_Comparecompare

所以你基本上有

const Student& std::__median(const Student&, const Student&, const Student&, 
                                                   bool (*)(Student&, Student&) )

或类似的。显然,回调不能应用于参数被转换为const,所以失败。

为您的compare方法设置参数const

于 2012-05-27T20:29:25.857 回答
2

我不相信标准中有要求函数的参数必须是 const,所以我相信你的实现拒绝它是错误的。但是,要求函数不修改参数:

从标准——25.4/2

Compare 是一个函数对象类型 (20.8)。应用于比较类型对象的函数调用操作的返回值,当上下文转换为 bool (4) 时,如果调用的第一个参数小于第二个参数,则返回 true,否则返回 false。比较 comp 始终用于假设排序关系的算法。假定 comp 不会通过取消引用的迭代器应用任何非常量函数。

std::sort以及来自 25.4.1.1的签名

template<class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)

因此,由于不允许您的函数修改其参数,因此它确实应该将它们作为 const 接受,但标准并不要求这样做。因此,虽然您的实现可能有错误,但我认为这是一个可以原谅的错误,因为它设法引起人们注意这样一个事实,即您的函数通过修改其参数违反了标准,或者它不是 const 正确的。

于 2012-05-27T20:44:57.720 回答
0

您应该将其设为 const,因为它使两个参数保持不变。

于 2012-05-27T20:18:23.417 回答