服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - c++ qsort 与sort 对结构体排序实例代码

c++ qsort 与sort 对结构体排序实例代码

2021-10-07 16:34RioTian C/C++

这篇文章主要介绍了c++ qsort 与sort 对结构体排序实例代码,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下

?
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

延伸 · 阅读

精彩推荐