简体   繁体   English

关于std :: list,如果我想按降序排序,可以简单地调用“ mylist.sort(>)”吗?

[英]Regarding std::list, if I want to sort in descending order can I simply call “mylist.sort(>)”?

假设列表包含的任何对象都实现了一个适当的>运算符。

Use : 采用 :

mylist.sort(std::greater<T>());

T is type of container elements. T是容器元素的类型。 eg. 例如。 int , char , float , etc. intcharfloat等。

For T as objects you need to overload operator '>' or define your own comparator function 对于T作为对象,您需要重载operator '>'或定义自己的比较器函数

bool cmp(const T &lhs, const T &rhs)
{

  //compare criteria 
}

And then, 接着,

mylist.sort(cmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM