[英]STL set custom sort
當我意識到不可能重新使用一個集合時,我不得不嘗試重新設置一個集合,而我不得不創建一個新集合並具有一個自定義排序函數來重新使用它 。 我在網上進行了研究,並嘗試實現自己的自定義排序功能,但不確定如何處理
這是我的課
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;
};
目前,它是根據x值和y值排序的,我想根據
y值,然后是x值
因此,我實現了這種自定義排序
bool p2d_sortby_y(Point2D& ptd1 , Point2D& ptd2) //custom sort function
{
if ( ptd1.getY() != ptd2.getY())
{
return ptd1.getY() < ptd2.getY();
}
if ( ptd1.getX() != ptd2.getX() )
{
return ptd1.getX() < ptd2.getX();
}
return false;
}
這是我如何嘗試使用集合的示例代碼,
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <cmath>
using namespace std;
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;
};
bool p2d_sortby_y(Point2D& ptd1 , Point2D& ptd2) //custom sort function
{
if ( ptd1.getY() != ptd2.getY())
{
return ptd1.getY() < ptd2.getY();
}
if ( ptd1.getX() != ptd2.getX() )
{
return ptd1.getX() < ptd2.getX();
}
return false;
}
int main()
{
set<Point2D> p2d_set;
Point2D p2d;
p2d.setX(1);
p2d.setY(3);
p2d_set.insert(p2d);
p2d.setX(3);
p2d.setY(2);
p2d_set.insert(p2d);
set<Point2D>::iterator p2 = p2d_set.begin();
while ( p2 != p2d_set.end() )
{
cout<<p2->getX()
<<" "
<<p2->getY()
<<endl;
p2++;
}
set<Point2D,p2d_sortby_y> p2d_set2 = p2d_set; // i am unsure of how to implement the custom sort function here
}
int Point2D::getX() const
{
return x;
}
int Point2D::getY() const
{
return y;
}
void Point2D::setX(int x1)
{
x = x1;
}
void Point2D::setY(int y1)
{
y = y1; ;
}
有人可以幫幫我嗎?
這將是一種更簡單的方法:
#include <tuple>
struct SortByYX
{
bool operator ()(const Point2D& lhs, const Point2D& rhs) const
{
return std::tie(lhs.y, lhs.x) < std::tie(rhs.y, rhs.x);
}
};
然后
set<Point2D, SortByYX> p2d_set2(p2d_set.begin(), p2d_set.end());
編輯 : std::tie
需要C ++ 11支持,但是如果沒有它,則可以使用<tr1/tuple>
std::tr1::tie
,或者如果沒有TR1則使用boost::tie
。
在set<Point2D, p2d_sortby_y>
,第二個參數p2d_sortby_y
沒有命名類型。
struct point_odered_by_y
{
bool operator()(const Point2D& ptd1, const Point2D& ptd2) const
{
/* .. */
}
};
typedef std::set<Point2D, point_odered_by_y> pointset;
pointset s(begin(p2d_set), end(p2d_set));
通常最好定義一個仿函數類來指定順序,而不是函數。 這樣,您可以將其指定為模板參數:
struct p2d_sortby_y {
bool operator()(Point2D x, Point2D y) {return whatever;}
};
這必須通過值或const
引用(而不是像您一樣的非const
引用)接受其參數,才能在常量集合成員上使用。
您不能直接從其他類型的集合中初始化新集合,但是可以從其范圍初始化它:
set<Point2D,p2d_sortby_y> p2d_set2(p2d_set.begin(), p2d_set.end());
兩組具有不同的類型(比較功能是類型定義的一部分)。
由於std::set
沒有為不同的比較函數提供構造函數或賦值運算符,因此您必須使用對第一個集合的迭代器對來初始化新集合,或者在元素上進行std::copy
。
那是:
set<Point2D,p2d_sortby_y> p2d_set2 { std::begin(p2d_set), std::end(p2d_set) };
要么
set<Point2D,p2d_sortby_y> p2d_set2;
std::copy(std::begin(p2d_set), std::end(p2d_set), std::inserter(p2d_set2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.