C++排序矢量对象错误

C++ sorting vector objects error

本文关键字:对象 错误 排序 C++      更新时间:2023-10-16

很抱歉发布了重复的帖子,但我尽我所能找到答案,仍然无法解决错误。我举了很多例子,但似乎都不起作用。我对物体的矢量排序有问题。

它在那行代码中给了我一大块错误,我甚至无法调试。当我把那行注释掉时,它运行得很好,没有错误。

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

首先,这不是一个可编译的最小示例。如果你能发布一些我们可以编译的东西(在这里说: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