码迷,mamicode.com
首页 > 其他好文 > 详细

CPP - sort

时间:2016-06-06 20:31:43      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class student{
public:
	student(){}
	student(int num, string str, int s)	{id= num;name = str;score = s;	}
	student(student &stu){id = stu.id;name = stu.name;score = stu.score;}
	int getid(){ return id; }
	string getname(){ return name; }
	int getscore(){ return score; }
	bool operator < (const student &stu)const { return score < stu.score; }
	bool operator > (const student &stu){ return score > stu.score; }
	bool operator () (const student &stu1, const student &stu2){ return stu1.score > stu2.score; }
private:
	int id;
	string name;
	int score;
};

int cmp( const void *a ,const void *b)  
{  
return ((student *)a)->getscore() - ((student *)b)->getscore() ;  
}


int main() {
	int i;
	student stu[5] = { student(1, "zhu", 86),student(2, "xing", 92),
	student(3, "zhao", 78),student(4, "fu", 88),student(5, "liu", 76) };
	qsort(stu,5,sizeof(stu[0]),cmp);
	for(i=0;i<5;i++)
		cout << stu[i].getid() <<" "<< stu[i].getname()<<" " << stu[i].getscore() << endl;
	return 0;
}

  

CPP - sort

标签:

原文地址:http://www.cnblogs.com/zhangyifang/p/5564929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!