[英]C++ Operator overload: stl sort on vector of my custom class
我有一個非常基本的類存儲在STL Vector中。 我試圖對該向量進行排序,但我得到了神秘的STL錯誤。 有人可以幫忙嗎?
// Point.h
class Point {
public:
Point() : x(0), y(0) {}
Point( float x0, float y0 ) : x(x0), y(y0) {}
float x;
float y;
};
// Point.cpp, updated const as per given answers
bool operator< (const Point &p1,const Point &p2)
{
return p1.x < p2.x || (p1.x==p2.x && p1.y< p2.y);
}
同樣,此Point類存儲在向量中並正在排序:
std::vector<Point> tmp=N->points;
std::sort(tmp.begin(),tmp.end());
錯誤:
有人能指出我正確的方向嗎? 謝謝!
bool operator< (
const
Point &p1,
const
Point &p2 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.