C++中sort()函数的greater<int>()参数

目录

  • [1 基础知识](#1 基础知识)
  • [2 模板](#2 模板)
  • [3 工程化](#3 工程化)

1 基础知识

sort()函数中的greater<int>()参数表示将容器内的元素降序排列。不填此参数,默认表示升序排列。

cpp 复制代码
vector<int> a = {1,2,3};
sort(a.begin(), a.end(), greater<int>()); //将a降序排列
sort(a.begin(), a.end()); //将a升序排列

2 模板

cpp 复制代码
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    vector<int> a = {8,3,4,5,6};
    sort(a.begin(), a.end(), greater<int>());
    
    for (auto x : a) {
        cout << x << " ";
    }
    cout << endl;
    
    return 0;
}

上述程序输出,

txt 复制代码
8 6 5 4 3 

3 工程化

暂无。。。

相关推荐
纵有疾風起13 分钟前
C++——多态
开发语言·c++·经验分享·面试·开源
冯诺依曼的锦鲤26 分钟前
算法练习:差分
c++·学习·算法
Mr_WangAndy1 小时前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
氵文大师1 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
封奚泽优1 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER1 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
笃行客从不躺平2 小时前
遇到大SQL怎么处理
java·开发语言·数据库·sql
郝学胜-神的一滴2 小时前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
g***B7382 小时前
Kotlin协程在Android中的使用
android·开发语言·kotlin
火白学安全2 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全