C++ 对struct的向量进行排序会导致VisualStudio抛出一个带有“;“调试断言失败”;
我有一个简单的矩形结构,有x,y,width和height,我认为使用向量很容易,但结果是非常混乱 这就是我的主要观点:C++ 对struct的向量进行排序会导致VisualStudio抛出一个带有“;“调试断言失败”;,c++,sorting,stl,opencv,C++,Sorting,Stl,Opencv,我有一个简单的矩形结构,有x,y,width和height,我认为使用向量很容易,但结果是非常混乱 这就是我的主要观点: vector<CvRect> v; v.push_back(cvRect(50,0,50, 50)); v.push_back(cvRect(150, 0, 50,50)); v.push_back(cvRect(100, 0, 50,50)); sort(v.begin(), v.end(), rectangleCmpByPosition); 定义如下: st
vector<CvRect> v;
v.push_back(cvRect(50,0,50, 50));
v.push_back(cvRect(150, 0, 50,50));
v.push_back(cvRect(100, 0, 50,50));
sort(v.begin(), v.end(), rectangleCmpByPosition);
定义如下:
static int rectangleCmpByPosition(const CvRect& a, const CvRect &b){
if (a.y != b.y){
return a.y - b.y;
}else{
return a.x - b.x;
}
}
VisualStudio会向我抛出此错误消息
我花了整个下午的时间在谷歌上搜索我做错了什么,但我找不到原因。请帮助。您的比较器不正确。如果
a
小于b
,则比较器需要返回bool
:true
,否则返回false
。它需要提供一个。您的比较器不正确。如果a
小于b
,则比较器需要返回bool
:true
,否则返回false
。它需要提供一个。谢谢您的帮助。我知道这很愚蠢。谢谢你的帮助。我知道这是很愚蠢的事情。
static int rectangleCmpByPosition(const CvRect& a, const CvRect &b){
if (a.y != b.y){
return a.y - b.y;
}else{
return a.x - b.x;
}
}