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

暂无。。。

相关推荐
wm104313 小时前
代码随想录第十天 栈和队列
开发语言·python
Java后端的Ai之路14 小时前
【Java教程】- 并发编程核心知识解读
java·开发语言·并发编程
Sheep Shaun14 小时前
C++11核心特性详解:从右值引用到现代C++编程
开发语言·数据结构·c++·算法
Dontla14 小时前
Mybatis Introduction (Java ORM Framework)
java·开发语言·mybatis
信码由缰14 小时前
JExten:基于Java模块系统(JPMS)构建健壮的插件架构
java·开发语言·架构
Dxy123931021614 小时前
Python使用Playwright入门教程:从环境搭建到实战应用
开发语言·python·playwright
小王努力学编程14 小时前
LangChain——AI应用开发框架
服务器·c++·人工智能·分布式·rpc·langchain·brpc
呱呱巨基14 小时前
Linux Ext系列文件系统
linux·c++·笔记·学习
云深麋鹿14 小时前
三.栈和队列
开发语言·数据结构·c++·算法
爆打维c14 小时前
01BFS算法(例题:网格传送门旅游)
c语言·c++·python·算法·leetcode·广度优先