#include<iostream>
#include<algorithm>
#include<functional>
#include<set>
using namespace std;
class Widget
{
public:
size_t weight() const
{
}
size_t maxSpeed() const
{
}
bool operator<(const Widget& w)
{
return this->weight() < w.weight();//这是按照重量从小到大排序
}
private:
};
/*
但是在某种情况下,我们需要创建一个按照最大速度进行排序的multiset<Widget>容器.
我们知道,mutiset<Widget>的默认比较函数是less<Widget>,而less<Widget>默认
情况下会调用operator<来完成自己的工作,为实现按照最大速度排序,一种可能的方法
是特化less<widget>,切断less<Widget>和operator<之间的关系
*/
template<typename T>
struct std::less<Widget>:
public std::binary_function<Widget,Widget,bool>{
bool operator()(const Widget& lhs, const Widget&rhs)
{
return lhs.maxSpeed() < rhs.maxSpeed();
}
};
//作为一般性原则,对STD名字空间的组件进行修改确实是禁止的,但是某些情况下,又是允许的
//程序员可以针对用户定义的类型,特化std中的模板:例如:智能指针类的std:less特化版本
//并不少见
namespace std{
template<typename T>
struct less<boost::shared_ptr<T>>:
public
binary_function<boost::shared_ptr<T>, boost::shared_ptr<T>, bool>{
bool operator()(const boost::shared_ptr<T>& a, const boost::shared_ptr<T>&b)const
{
return less<T*>()(a.get(), b.get());//shared_ptr::get返回的shared_ptr对象的内置指针
}
};
}
//可以使用另一种方法来完成上边的问题
struct MaxSpeedCompare :
public binary_function<Widget, Widget, bool>{
bool operator()(const Widget& lhs, const Widget& rhs)const
{
return lhs.maxSpeed() < rhs.maxSpeed();
}
};
int main()
{
multiset<Widget, MaxSpeedCompare> wisgets;//使得该容器按照规定的排序方法进行排序
multiset<Widget> wisgets2;//该容器是使用less<Widget>进行排序的
return 0;
}
应该尽量避免修改less的行为,因为这样做很有可能误导其他的程序员。如果使用了less,无论是显式还是隐式,你都需要确保她与operator<有相同的意义。若希望以一种特殊的方式来排序对象,那么最好创建一个特殊的函数子类,它的名字不能是less,这样做其实是很简单的。