2011年6月18日土曜日

構造体のvector配列におけるsort

評価関数を別に作るらしい
struct Point2D {
     int x;
     int y;
}

vector<Point2D> pt;

bool ascending_x_Point2D (const Point2D &_p1, const Point2D &_p2)
{
     return _p1.x < _p2.x;
}


bool ascending_y_Point2D (const Point2D &_p1, const Point2D &_p2)
{
     return _p1.y < _p2.y;
}


sort(pt.begin(), pt.end(), ascending_xPoint2D);


※ソートの範囲をしているするには
sort(&pt[0], &pt[5], ascending_xPoint2D);
のように、ソートの始めと終わりのアドレスを渡す。




-----
参照:http://atashi.net/inu/ja/notes/stl_vector_sort.html

0 件のコメント:

コメントを投稿