C ++操作符重载:stl排序我的自定义类的向量 [英] C++ Operator overload: stl sort on vector of my custom class

查看:146
本文介绍了C ++操作符重载:stl排序我的自定义类的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的类存储在STL向量。我试图排序该向量,但我得到神秘的STL错误。有人可以协助吗?

I have a very basic class that is stored inside an STL Vector. I am trying to sort that vector, but am getting cryptic STL errors. Can someone assist?

// 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类存储在一个向量中, / p>

Again, this Point class is stored in a vector and is being sorted:

std::vector<Point> tmp=N->points;
std::sort(tmp.begin(),tmp.end());

错误:

http://ideone.com/WIv0u

有人可能指向我正确的方向?谢谢!

Can someone point me in the right direction? Thanks!

推荐答案

bool operator< ( const 点&p1, const Point& p2)

这篇关于C ++操作符重载:stl排序我的自定义类的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆