std::lower_boundについてのメモ
std::lower_boundを使おうとしたところで疑問、
struct some_huge_structA
{
int value;
int other1;
int other2;
//and many member
};
みたいな構造体のvalueでsortされていて、valueについての比較のもと、
lower_boundで取得したいとき。
先ず、lower_boundの定義
std::lower_bound(_FwdIt _First, _FwdIt _Last, const _Ty &_Val);
std::lower_bound(_FwdIt _First, _FwdIt _Last, const _Ty &_Val, _Pr _Pred);
3引数の場合は、比較しようとするたびにsome_huge_structAが必要なので、
ありえない。
4引数の場合、
bool operator()(const some_huge_structA& lhs, int rhs)const;
を取るプレディケータを用意するけれども、
some_huge_structAとintの型を逆にして比較することや、
some_huge_structAのみを使ってのsortの整合性判定があるので(Debugのみ?)
struct cmp
{
bool operator()(const some_huge_structA& lhs, int rhs) const { return lhs.value < rhs; }
bool operator()(int rhs, const some_huge_structA& lhs) const { return rhs < lhs.value; }
bool operator()(
const some_huge_structA& lhs,
const some_huge_structA& rhs) const
{ return lhs.value < rhs.value; }
};
こんな感じのファンクタを4引数のlower_boundの_Predに渡してやれば万事解決。
visual studio 2008にて。。。