前序工作(宏定义,typedef,函数等)

本文介绍了一种使用C++实现的排序算法,通过定义自定义比较器cmp,并使用标准模板库(STL)中的sort函数对整数向量进行降序排序。此外,还展示了如何自定义输入和输出操作符以方便地读取和打印向量及集合。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <queue>
 9 #include <cmath>
10 
11 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
12 #define _rep(i,a,b) for(int i = (a);i <= (b);i ++)
13 #define push_back pb
14 typedef long long int ll;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
15 
16 using namespace std;
17 
18 struct cmp
19 {
20     inline bool operator() (const int a,const int b)
21     {
22         return a >= b;
23     }
24 };
25 
26 int readint() {int x;scanf("%d",&x);return x;}//vector<int> v;v_push_back(readint())
27 
28 template<typename T>
29 ostream& operator <<(ostream& os,vector<T> &v) {_for(i,0,v.size()) os << v[i] << " "; os << endl;return os;}
30 template<typename T>
31 ostream& operator <<(ostream& os,set<T> &v) {for(auto it = v.begin();it != v.end();it ++) os << *it << " ";os << endl;return os;}
32 int main()
33 {
34     vector<int> v {2,5,2,6,3,3,8,3,7,92,6,323,39,234,32,454,0};
35     sort(v.begin(),v.end(),cmp());
36     
37     _for(i,0,v.size())
38         cout << v[i] << " ";
39     cout << endl;
40     
41     cout << v;
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/Asurudo/p/9729257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值