首页 > 学院 > 开发设计 > 正文

greater和less的用法

2019-11-08 02:57:49
字体:
来源:转载
供稿:网友

greater使内置类型从大到小排序,而less从小到大

注意: sort用greater排序,则a[0]到a[n]有大到小排序。 PRiority_queue用greater排序,则先取出的是最小值。

#include <iostream> #include <algorithm> using namespace std; int main() { int a[5] = {4, 1, 5, 3, 2}; sort(a, a + 5, greater<int>()); //由大到小排序 for(int i = 0; i < 5; i++) cout << a[i] << " "; cout<<"/n"; sort(a, a + 5, less<int>()); //由小到大排序(sort一般默认为从小到大,所以less可以省略) for(int i = 0; i < 5; i++) cout<<a[i]<<" "; return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表