对于自定义的比较函数,我之前以后是迷惑的,cmp和仿射函数struct cmp有什么区别?什么时候使用cmp,什么时候使用仿射函数。

答:在sort函数中,我们能够对vector,string,deque等进行排序,在对上述STL容器进行排序时,需要写cmp函数,而像map,set,multiset,priority_queue等内部使用红黑树进行排序的容器,不能使用sort进行排序,也就不能使用cmp函数,这个时候就需要写放射函数来实现内部的元素的排序。

bool cmp(const int &a,const int &b){//const 和 &可以不写
return a<b;
}
struct cmp{
bool operator()(const int &a,const int &b){
return a<b;
}
};