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

暂无。。。

相关推荐
感哥8 小时前
C++ 多态
c++
沐怡旸15 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River41618 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥20 小时前
C++ std::set
c++
侃侃_天下21 小时前
最终的信号类
开发语言·c++·算法
博笙困了21 小时前
AcWing学习——差分
c++·算法
echoarts21 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式