信息发布→ 登录 注册 退出

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

发布时间:2026-01-10

点击量:
#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 对结构体排序的资料请关注其它相关文章!

标签:# namespace  # main  # num  # lt  # include  # stdc  # bits  # class  # pre  # cpp  # brush  # string  # struct  # int  # book  # c++  # gt  # typedef  # std  # 更多关于  # 相关文章  # 多条件)  # c++结构体排序方式(1条件  # 排序等操作详解  # 删除  # c++ STL之list对结构体的增加  # C++中的结构体vector排序问题  # 结构体排序  # sort  # qsort  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!