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

暂无。。。

相关推荐
吃我一窝jio6 小时前
Python与Go线程模型深度对比
java·开发语言·python
eggwyw7 小时前
C++ union 的一种妙用法
开发语言·c++
ShineWinsu8 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
starzy19909 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
小七在进步9 小时前
string的实现(1)
c++·笔记·visual studio
布莱克60511 小时前
Swift 循环与控制转移详解:从 for-in 到标签跳转
开发语言·ssh·swift·循环
敲上瘾12 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
联犀12 小时前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
牢姐与蒯13 小时前
cpp算法之哈希
c++·算法·哈希算法
lcj251114 小时前
[C++] C++11
开发语言·c++·笔记