<>先来看一个每日一题

[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
class Solution { public: vector<vector<int>> reconstructQueue(vector<vector<int
>>& people) { int n=people.size(); vector<vector<int>>ans; sort(people.begin(),
people.end(),[&](vector<int>&x,vector<int>&y)->bool{return x[0]>y[0]||(x[0]==y[0
]&&x[1]<y[1]);}); vector<int>idx; for(int i=0;i<n;i++){ idx.insert(idx.begin()+
people[i][1],i); } for(int i=0;i<n;i++){ ans.push_back(people[idx[i]]); } return
ans; } };

<>进入正题

1.sort自定义排序

sort()排序函数是c++头文件include 中的函数，它采用的是一种类似于快排的排序方式，时间复杂度 n*log2(n)。可以对

2.使用lambda函数
vector<vector<int>> people{ {7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2} }; bool
campare(vector<int> A, vector<int> B) { if (A[0] > B[0]) { return true; } else
if(A[0]==B[0]) { return A[1] < B[1]; } return false; } void print(vector<vector<
int>> people) { for (auto it : people) { cout << it[0] << " " << it[1] << endl;
} } int main() { vector<vector<int>> test = people; //1.默认排序（升序） sort(test.begin
(), test.end()); printf("******默认排序********\n"); print(test); //2.compare排序 test
= people; sort(test.begin(), test.end(), campare); printf(
"******compare排序********\n"); print(test); //3.lambda排序 test = people; sort(test
.begin(), test.end(), [&](vector<int> A, vector<int> B) { if (A[0] > B[0]) {
return true; } else if (A[0] == B[0]) { return A[1] < B[1]; } return false; } );
printf("******lambda排序********\n"); print(test); }

4 4
5 0
5 2
6 1
7 0
7 1
compare排序**
7 0
7 1
6 1
5 0
5 2
4 4
lambda排序**
7 0
7 1
6 1
5 0
5 2
4 4

sort中的比较函数compare要声明为静态成员函数或全局函数，不能作为普通成员函数，否则会报错。 invalid use of non-static
member function

GitHub

Gitee