独学で STL を学ぼうとしているときに、次のクラスを作成しました。
class Person{
public:
...
bool operator<(const Person& p) const; // sorts by weight
friend bool compareWeight(const Person &p, const int& wt); //true if wt<p.weight
friend bool compareWeight(const int& wt, const Person &p);
...
private:
int age;
int weight;
};
Operator< は次のように定義されます。
bool Person::operator<(const Person& p) const{
if (weight<p.weight)
return true;
else
return false;
}
これが機能しない理由:
// get lower_bound for weight = 50
vector<Person>::iterator itr = lower_bound(v.begin(),v.end(),50,compareWeight);
それはスローします:
error C2914: 'std::lower_bound':cannot deduce template argument as function argument is ambiguous
体重 = 50 のダミーの人を使用してこれを解決し、lower_bound を呼び出します。
vector<Person>::iterator itr = lower_bound(v.begin(),v.end(), dummy);
しかし、明らかにあまりエレガントではありません。誰かがcompareWeightを機能させるのを手伝ってくれますか? また、そのような場合の最善のアプローチに関する提案は素晴らしいでしょう。Boost または C++11 はありません。申し訳ありません。