構造体
struct A {...};
を格納したSTLコンテナ
(例)std::vector<A> a;
を<algorithm>のsort関数を用いて行う方法。
以下の三つを紹介
- 比較演算子のオーバーロード
- 比較関数
- 関数オブジェクト
比較演算子のオーバーロード
比較演算子
bool operator<(const A &left, const A &right) {...}
bool operator>(const A &left, const A &right) {...}
をオーバーロードする。ソートのときは
std::sort(a.begin(), a.end());
とする。
比較関数
比較関数
bool compare(const A &left, const A &right) {...}
を定義する。関数名compareは何でもいい。ソートの時は
std::sort(a.begin(), a.end(), compare);
とする。
関数オブジェクト
operator()を持つ関数オブジェクト
class compare {
bool operator()(const A &left, const A &right) {...}
};
を定義する。クラス名compareは何でもいい。ソートの時は
std::sort(a.begin(), a.end(), compare);
とする。
最終更新:2011年01月31日 16:23