标签:type 结构体排序 auto str sizeof ace int space void
#include<bits/stdc++.h>
using namespace std;
typedef struct {
string book;
int num;
}Book;
//qsort的比较函数
int cmp(const void * a, const void * b) {
return (*(Book*)a).num > (*(Book*)b).num ? 1 : 0;
}
//sort的比较函数
bool cmp_(Book a, Book b) {
return a.num > b.num;
}
int main() {
Book Bok[3] = { {"1",4},{"2",2},{"3",3} };
cout << endl << "----------------" << "qsort函数" << endl;
qsort(Bok, 3, sizeof(Bok[0]),cmp);
for (auto i : Bok) {
cout << i.num << endl;
}
cout << "----------------" << "sort函数" << endl;
sort(Bok, Bok + 3, cmp_);
for (auto i : Bok) {
cout << i.num << endl;
}
return 0;
}
标签:type 结构体排序 auto str sizeof ace int space void
原文地址:https://www.cnblogs.com/Kanna/p/12350603.html