【问题标题】:C++ sorting vector objects error [duplicate]C ++排序矢量对象错误[重复]
【发布时间】:2015-04-07 17:01:23
【问题描述】:

很抱歉发布了重复的帖子,但我尽我所能找到答案仍然无法解决错误。我遵循了很多例子,但它似乎不起作用。我在对对象向量进行排序时遇到问题。

它在该行代码上给了我一大堆错误,我什至无法调试。 当我注释掉该行时,它运行良好而没有错误。

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

【问题讨论】:

  • return d1.getCivIndex() &gt; d2.getCivIndex()怎么样
  • 你能告诉我们你得到的“全部错误”是什么吗?
  • @Brandon 刚刚更新了错误信息

标签: c++ sorting


【解决方案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

【讨论】:

  • 该函数不需要将其参数作为const,但不应修改。
  • MissionPlan.cpp:在函数'bool sortByCiv(const PointTwoD&,const PointTwoD&)'中:MissionPlan.cpp:25:24:错误:将'const PointTwoD'作为'this'参数传递'float PointTwoD ::getCivIndex()' 丢弃限定符 [-fpermissive] return d1.getCivIndex() > d2.getCivIndex();
  • 好吧,我猜这个库确实要求参数是const
  • 另外,getCivIndex() 必须是常量。
  • @KhouriGiordano 你的意思是当我在我的头文件和 cpp 文件中实现它们时?
猜你喜欢
  • 2012-12-14
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
相关资源
最近更新 更多