C ++排序矢量对象错误

时间:2015-04-07 17:01:24

标签: c++ sorting

很抱歉发布重复的帖子,但我尽力找到答案仍然无法解决错误。我遵循了很多例子,但它似乎并没有起作用。我有排序对象矢量的问题。

它在那行代码中给了我一大堆错误,而我甚至无法调试。 当我注释掉那条线时,它没有错误地运行良好。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

//some other codes

vector<PointTwoD> Point;

bool sortByCiv(PointTwoD &d1, PointTwoD &d2) { 
    return d1.getCivIndex() < d2.getCivIndex(); 
}


void print_top5()

{

    int num = 0;;
    cout << "Total no. of records available = " << Point.size() << endl;

    cout << "Prining top 5 explorations destinations..." << endl;
    **sort(Point.begin(), Point.end(), sortByCiv);**
    for (int i = 0; i < 5; i++)

    {
        num++;
        int x, y;
        float civ;
        x = Point[i].getX();
        y = Point[i].getY();
        civ = Point[i].getCivIndex();
        if (civ > 0){    
            cout<< num << " Civ Index :" << civ << " at sector (" << x << ", " << y << ")" << endl;
        } else {
            cout<< num << " <no other records available>" << endl;
        }


    }

}

我能够在for循环中很好地打印出结果,我只需要按降序排列。非常感谢。

这是错误消息

In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from MissionPlan.cpp:6:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >; _Tp = PointTwoD; _Compare = bool (*)(PointTwoD&, PointTwoD&)]’:
/usr/include/c++/4.8/bits/stl_algo.h:2296:78:   required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >; _Compare = bool (*)(PointTwoD&, PointTwoD&)]’
/usr/include/c++/4.8/bits/stl_algo.h:2337:62:   required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >; _Size = long int; _Compare = bool (*)(PointTwoD&, PointTwoD&)]’
/usr/include/c++/4.8/bits/stl_algo.h:5490:44:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >; _Compare = bool (*)(PointTwoD&, PointTwoD&)]’
MissionPlan.cpp:33:44:   required from here
/usr/include/c++/4.8/bits/stl_algo.h:2263:35: error: invalid initialization of reference of type ‘PointTwoD&’ from expression of type ‘const PointTwoD’
    while (__comp(*__first, __pivot))
                                   ^
/usr/include/c++/4.8/bits/stl_algo.h:2266:34: error: invalid initialization of reference of type ‘PointTwoD&’ from expression of type ‘const PointTwoD’
    while (__comp(__pivot, *__last))

1 个答案:

答案 0 :(得分:1)

首先要做的事情 - 这不是最小的可编译示例。如果你可以发布我们可以编译的东西(比如这里:http://www.tutorialspoint.com/compile_cpp11_online.php),它会有很多帮助。

话虽如此,你的比较函数应该通过const引用来获取参数。另外,为了按降序排序,您需要更改谓词的逻辑,如下所示:

bool sortByCiv(const PointTwoD &d1, const PointTwoD &d2) { 
    return d1.getCivIndex() > d2.getCivIndex(); 
}

编辑:

看一下这个链接 - 这段代码会编译,您可以将它作为参考。

http://goo.gl/kUVP5r