Sorting a vector of struct causes Visual Studio to throw a popup with "Debug Assertion Failed"
I have a simple Rectangle struct with x, y, width, and height, which I thought would be easy to work with vectors but it turned out to be pretty messy.
This is what I have in main:
vector<CvRect> v;
v.push_back(cvRect(50,0,50, 50));
v.push_back(cvRect(150, 0, 50,50));
v.push_back(cvRect(10开发者_如何转开发0, 0, 50,50));
sort(v.begin(), v.end(), rectangleCmpByPosition);
I have this declared in my header
static int rectangleCmpByPosition(const CvRect &a, const CvRect &b);
with this as definition:
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;
}
}
And visual studio throws this error message at me
.I spent the whole afternoon googling to see what I did wrong but I can't find the cause. Please help.
Your comparator is incorrect. The comparator needs to return a bool
: true
if a
is "less than" b
and false
otherwise. It needs to provide a strict weak ordering.
精彩评论