如何在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
热门推荐
3 入学校简短祝福语
10 早餐祝福语怎么写简短
11 简短祝福语蛋糕简笔画
12 道教生日祝福语简短大全
13 团体年会祝福语简短霸气
14 姨父生日祝福语简短精辟
15 长辈生日横幅祝福语简短
16 感谢母校寄语简短祝福语
17 叔叔生日祝福语简短搞笑
18 新年元旦祝福语大全简短