C++标准库是否具有"有序集"数据结构?通过有序集合,我的意思是与普通的完全相同,std::set但是它会记住您将项目添加到其中的顺序.
如果没有,模拟一个的最佳方法是什么?我知道你可以做一些事情就像有一对配对,每一对都存储它被添加的数字和实际值,但如果有一个更简单的解决方案,我不想跳过箍.
当我意识到不可能使用一套装置而我必须创建一个新装置并拥有一个自定义排序功能来诉诸它时,我试图求助于一套.我在网上研究并试图实现我自己的自定义排序功能,但我不知道如何去做
这是我的班级
class Point2D
{
public:
int getX() const;
int getY() const;
void setX(int);
void setY(int);
bool operator < ( const Point2D& x2) const
{
if ( x != x2.x)
{
return x < x2.x;
}
if ( y != x2.y)
{
return y < x2.y;
}
};
protected:
int x;
int y;
};
Run Code Online (Sandbox Code Playgroud)
目前它是根据x值后跟y值排序,我想根据它来求助它
y值后跟x值
因此我实现了这种自定义排序
bool p2d_sortby_y(Point2D& ptd1 , Point2D& ptd2) //custom sort function
{
if ( ptd1.getY() != ptd2.getY())
{
return ptd1.getY() < …Run Code Online (Sandbox Code Playgroud)