C++ sort()函数的cmp含义

<algorithm>

   std::sort(first,last,cmp); 

  使用的范围是[first,last)

  • 省略 cmp,使用 sort(first,last), 则默认从 小到大排序
  • 使用 sort(first,last, greater<T>() ), 则 从 大到小排序
  • 如果是结构体或者自定义排序规则,则需要自定义cmp 函数。

  cmp函数的含义,如果返回值是 True,表示 要把 序列 (X,Y),X放Y前。

bool cmp(int &x,int &y){ 
  return x>y;//意味着x>y的时候,把x放到y前,按大到小排序。
}

 

 

猜你喜欢

转载自www.cnblogs.com/--zz/p/10464751.html