参考:
http://blog.****.net/lihao21/article/details/6302196
/* class for function predicate
* - operator() returns whether a person is less than another person
*/
class PersonSortCriterion {
public:
bool operator() (const Person& p1, const Person& p2) const {
/* a person is less than another person
* - if the last name is less
* - if the last name is equal and the first name is less
*/
return p1.lastname()<p2.1astname() ||
(! (p2.1astname()<p1.lastname()) &&
p1.firstname()<p2.firstname());
}
};
set的find查找相等原理
http://bbs.****.net/topics/390237400
向set中添加的元素类型必须重载<操作符用来排序。排序满足以下准则:
1、非对称,若A<B为真,则B<A为假。
2、可传递,若A<B,B<C,则A<C。
3、A<A永远为假。
set中判断元素是否相等:
if(!(A<B || B<A)),当A<B和B<A都为假时,它们相等。
以上好处可以直接不通过指针而通过对象来查找
class Data
{
public:
Data(int view, const char* key)
: view_(view),
key_(key),
data_(NULL) {}
~Data() {} public:
int view_;
const char* key_;
void* data_;
}; class DataComparator {
public:
bool operator()(const Data* d1, const Data* d2) const {
return (d1->view_ == d2->view_) ? (d1->key_ < d2->key_) :
(d1->view_ < d2->view_);
}
}; typedef std::set<Data*, DataComparator> DataSet; void main()
{
DataSet dataSet;
dataSet.insert(new Data(1,"b"));
DataSet::iterator iter=dataSet.find(new Data(1,"b"));
}