-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumsUsage.cpp
More file actions
96 lines (73 loc) · 1.62 KB
/
numsUsage.cpp
File metadata and controls
96 lines (73 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Created by wbzhang on 2020/8/31.
//
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 自定义结构体的自定义比较函数
struct Point2i{
int x,y;
Point2i(int _x,int _y):x(_x),y(_y){ };
bool operator<(const Point2i &pt)
{
return this->y < pt.y;
}
};
// 比较函数
bool ptcmp(Point2i pt1,Point2i pt2)
{
return pt1.y<pt2.y;
}
// 结构体包装比较函数
struct sptcmp{
bool operator()(const Point2i &pt1,const Point2i &pt2){
return pt2.y < pt1.y;
}
}sptcmp1;
bool cmp(int arr[])
{
if(arr[0]>arr[1]){
return true;
}
return false;
};
bool myfunction (int i,int j) { return (i<j); }
struct mycmp{
bool operator()(const vector<int> &a,const vector<int> &b){
return a[0]<b[0];
};
};
bool cmp(vector<int> va, vector<int> vb){
if(va.size()==0) return true;
return va[0]<vb[0];
};
bool operator<(const vector<int> &a,const vector<int> &b){
return a[0]<b[0];
}
int main()
{
// 1.基本类型定义比较函数
int arr[2] = {1,2};
int *array = new int[2];
for(int i=0;i<2;i++){
array[i] = i;
}
vector<int> ar = {1,4,2,3,5};
sort(ar.begin(),ar.end(),myfunction); // 比较函数
/// 2.对结构体自定义比较函数
// vector<vector<int> > arr2 = {{1,4,5},{0,3,6},{9,8,5},{2,1,1},{0,3,6}};
// std::sort(arr2.begin(),arr2.end(),cmp);
// std::sort(arr2.begin(),arr2.end());
// 自定义结构体
vector<Point2i> pts;
for(int i=0;i<6;++i){
pts.push_back(Point2i(i,(i+1)*3) );
}
// 将pts按照y逆向排序
sort(pts.begin(),pts.end(),ptcmp);
sort(pts.begin(),pts.end());
sort(pts.begin(),pts.end(),sptcmp1);
return 0;
}