请教:sort函数排序错误
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)