当前位置:首页 > 通信资讯 > 正文

c语言qsort函数对结构体排序(sort函数排序结构体)

?
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 #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; }

以上就是c++ qsort 与sort 对结构体排序实例代码的详细内容,更多关于c++ qsort 与sort 对结构体排序的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/RioTian/p/12350603.html

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。