C++排序矢量对象错误
C++ sorting vector objects error
很抱歉发布了重复的帖子,但我尽我所能找到答案,仍然无法解决错误。我举了很多例子,但似乎都不起作用。我对物体的矢量排序有问题。
它在那行代码中给了我一大块错误,我甚至无法调试。当我把那行注释掉时,它运行得很好,没有错误。
#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
相关文章:
- 对象错误地显示为引用?
- 调用带有指针对象错误的指针变量
- 为什么谷歌测试/模拟显示 std::unique_ptr 泄露的模拟对象错误?
- C 数组指向对象错误
- C++:对象错误的多重定义
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- C 字符串对象错误字符
- 声明 Windows API 结构 (DCB) 的对象 - 错误 C4430:缺少类型说明符 - 假定为 int
- 无法调用成员函数而不出现对象错误
- C++指针/对象错误
- C++排序矢量对象错误
- C++:在映射中存储类对象(错误:获取类型为"Node"的临时对象的地址)
- 使用三元运算符初始化类对象错误
- 帧缓冲区对象错误
- 实例化新对象 - 错误
- C++打印出对象错误
- 如何更正类型对象错误
- 简单指针对象错误C++
- 运算符重载添加两个对象错误
- c++列表对象错误