请教:sort函数排序错误

ucfree86 2012-07-18 12:38:31
vEqualPair插入若干元素后,调用函数mConvertEqualPair2List,首先需要对vEqualPair进行排序。
使用STL的sort函数却报错,而自己写一个排序算法就能通过。
是不是我使用sort不正确了? 回调函数有问题吗?

void CEvConnectRegion::mConvertEqualPair2List(vector<pair<int, int> > &vEqualPair, vector<vector<int> > &vEqualList)
{
//
sort(vEqualPair.begin(), vEqualPair.end(), mEqPairSortFun);
...
}

bool CEvConnectRegion::mEqPairSortFun(pair<int, int> prX, pair<int, int> prY)
{
if(prX.first <= prY.first)
{
return TRUE;
}

return FALSE;
}

valgrind 调试信息如下:
24 ==5469== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
25 ==5470== Invalid read of size 4
26 ==5470== at 0x410F3B: _ZSt16__introsort_loopIN9__gnu_cxx17__normal_iteratorIPSt4pairIiiESt6vectorIS3_SaIS3_EEEElPFbS3_S3_EEvT_SB_T0_T1_.clone.1 (evconreg.cpp:256)
27 ==5470== by 0x41236B: CEvConnectRegion::mConvertEqualPair2List(std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allo cator<int> > > >&) (stl_algo.h:5258)
28 ==5470== by 0x4136C8: CEvConnectRegion::LabelRegion(cv::Mat&, int) (evconreg.cpp:538)
29 ==5470== by 0x40B41D: CEvfhSplit::mPrepSimpleColorImg() (evfhsplit.cpp:226)
30 ==5470== by 0x40E2F0: CEvfhSplit::Split2Char(std::vector<std::pair<cv::Rect_<int>, int>, std::allocator<std::pair<cv::Rect_<int>, int> > >&) (evfhsplit.cpp:143)
31 ==5470== by 0x405EB1: evfhproc(std::string&, std::string&, CEvfhSplit*, evrecog*) (evfhproc.cpp:49)
32 ==5470== by 0x405573: main (evfh.cpp:268)
33 ==5470== Address 0x893b158 is 8 bytes before a block of size 8,192 alloc'd
34 ==5470== at 0x4A06C8E: operator new(unsigned long) (vg_replace_malloc.c:261)
35 ==5470== by 0x40EF24: std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >, std::pair<int, int> const&) (new_allocator.h:89)
36 ==5470== by 0x41139A: CEvConnectRegion::mGetRunLable(cv::Mat&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, std ::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >&) (stl_vector.h:741)
37 ==5470== by 0x413698: CEvConnectRegion::LabelRegion(cv::Mat&, int) (evconreg.cpp:536)
38 ==5470== by 0x40B41D: CEvfhSplit::mPrepSimpleColorImg() (evfhsplit.cpp:226)
39 ==5470== by 0x40E2F0: CEvfhSplit::Split2Char(std::vector<std::pair<cv::Rect_<int>, int>, std::allocator<std::pair<cv::Rect_<int>, int> > >&) (evfhsplit.cpp:143)
40 ==5470== by 0x405EB1: evfhproc(std::string&, std::string&, CEvfhSplit*, evrecog*) (evfhproc.cpp:49)
41 ==5470== by 0x405573: main (evfh.cpp:268)

...全文
429 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Keep___Going 2012-07-19
  • 打赏
  • 举报
回复


看看这篇文章:http://wenku.baidu.com/view/e064166daf1ffc4ffe47ac67.html

你就明白了
mstlq 2012-07-19
  • 打赏
  • 举报
回复
ucfree86 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

可参考http://blog.csdn.net/bichenggui/article/details/4705472
[/Quote]

学习了 很感谢大家
ri_aje 2012-07-18
  • 打赏
  • 举报
回复
标准要求 std::sort 的比较仿函数定义严格弱序(strict weak ordering),把你比较函数中的 <= 改成 < 就行了。
ucfree86 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

bool CEvConnectRegion::mEqPairSortFun(const pair<int, int> &prX, constpair<int, int> & prY)
{
return prX.first <= prY.first;
}
[/Quote]

........... 一样的吧
bool宏没问题的
dahaiI0 2012-07-18
  • 打赏
  • 举报
回复
bool CEvConnectRegion::mEqPairSortFun(const pair<int, int> &prX, constpair<int, int> & prY)
{
return prX.first <= prY.first;
}
ucfree86 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

最好考虑下你的比较函数。最好是严格的弱排序
[/Quote]

不大明白你的意思,能举个例子么?

按道理说对vector<pair<int,int> >这样数据结构的排序很常见啊。。

请教你们都是怎么写的?
ucfree86 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你从提供的错误信息来看,是由于无效读错误:
Invalid read of size 4,而且总共出现6次,假设你的向量表中有4个元素的话,那你最大的问题应该在于
sort函数的比较函数,一般,对于对象比较,你应该提供的是引用参数,也就是:
bool CEvConnectRegion::mEqPairSortFun(pair<int, int> &prX, pair<int, i……
[/Quote]

sort回调函数的格式要求不能带引用的,使用了就语法错误了。
W170532934 2012-07-18
  • 打赏
  • 举报
回复
最好考虑下你的比较函数。最好是严格的弱排序
yin138 2012-07-18
  • 打赏
  • 举报
回复
你从提供的错误信息来看,是由于无效读错误:
Invalid read of size 4,而且总共出现6次,假设你的向量表中有4个元素的话,那你最大的问题应该在于
sort函数的比较函数,一般,对于对象比较,你应该提供的是引用参数,也就是:
bool CEvConnectRegion::mEqPairSortFun(pair<int, int> &prX, pair<int, int> &prY)
我不太确定问题所在,希望有所帮助。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧