y**b 发帖数: 10166 | 1 ///////////////////////////////////////////////////////////
A:
class Widget {
public:
...
size_t weight() const;
size_t maxSpeed() const;
...
};
bool operator<(const Widget& lhs, const Widget& rhs)
{
return lhs.maxSpeed() < rhs.maxSpeed();
}
multiset widgets;
///////////////////////////////////////////////////////////
B:
class Widget {
public:
...
size_t weight() const;
size_t maxSpeed() const;
...
};
struct MaxSpeedCompare:
public binary_function {
bool operator()(const Widget& lhs, const Widget& rhs) const
{
return lhs.maxSpeed() < rhs.maxSpeed();
}
};
multiset widgets;
这是effective stl item 42里面的内容。
A是默认定义形式,
B是可以用来扩展的定义形式,比如里面可以改成按weight进行比较? |
|