如何在C ++中使用STL按降序对Vector进行排序?
在本教程中,我们将讨论一个程序,以了解如何使用C++中的STL对向量进行降序排序。
为了按降序对给定向量进行排序,我们将使用sort()C++中STL库中的函数。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//收集向量
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//降序排列
sort(a.begin(), a.end(), greater<int>());
cout << "Sorted Vector in descending order: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
return 0;
}输出结果
Vector: 1 45 54 71 76 12 Sorted Vector in descending order: 76 71 54 45 12 1
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短