qsort对结构体排序

#include
#include

using namespace std;

struct node{
	int val;
	string s;
};

int cmp(const void*a,const void*b){
	return (*(node*)a).val - (*(node*)b).val;
}

int main(){
	node narr[20];
	narr[0].val = 20;
	narr[0].s = "Gao";
	narr[1].val = -10;
	narr[1].s = "Man";
	narr[2].val = 40;
	narr[2].s = "Li";
	qsort(narr,3,sizeof(node),cmp);
	for(int i = 0;i < 3;i++){
		cout<

你可能感兴趣的:(qsort对结构体排序)