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 工程化

暂无。。。

相关推荐
喜欢吃燃面3 小时前
Linux:环境变量
linux·开发语言·学习
徐徐同学4 小时前
cpolar为IT-Tools 解锁公网访问,远程开发再也不卡壳
java·开发语言·分布式
LawrenceLan4 小时前
Flutter 零基础入门(二十六):StatefulWidget 与状态更新 setState
开发语言·前端·flutter·dart
m0_748229994 小时前
Laravel8.X核心功能全解析
开发语言·数据库·php
qq_192779874 小时前
C++模块化编程指南
开发语言·c++·算法
代码村新手5 小时前
C++-String
开发语言·c++
qq_401700415 小时前
Qt 中文乱码的根源:QString::fromLocal8Bit 和 fromUtf8 区别在哪?
开发语言·qt
EndingCoder6 小时前
案例研究:从 JavaScript 迁移到 TypeScript
开发语言·前端·javascript·性能优化·typescript
Yyyyy123jsjs6 小时前
如何通过免费的外汇API轻松获取实时汇率数据
开发语言·python