CPP - sort
#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的更多相关文章
- DataStructure 排序 源码实现
本篇博客实现了 1.冒泡排序 2.冒泡排序的一种优化(当某次冒泡没有进行交换时,退出循环) 3.选择排序 4.归并排序 5.快速排序. 主要是源码的实现,并将自己在敲的过程中所遇到的一些问题记录下来. ...
- 简单排序算法 C++类实现
简单排序算法: 冒泡排序 插入排序 选择排序 .h代码: // // SortClass.h // sort and selection // // Created by wasdns on 16/1 ...
- 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂
C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 53. 特殊的O(n)时间排序[sort ages with hashtable]
[本文链接] http://www.cnblogs.com/hellogiser/p/sort-ages-with-hashtable.html [题目] 某公司有几万名员工,请完成一个时间复杂度为O ...
- HDU1890 Robotic Sort[splay 序列]
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 将三维空间的点按照座标排序(兼谈为std::sort写compare function的注意事项)
最近碰到这样一个问题:我们从文件里读入了一组三维空间的点,其中有些点的X,Y,Z座标只存在微小的差别,远小于我们后续数据处理的精度,可以认为它们是重复的.所以我们要把这些重复的点去掉.因为数据量不大, ...
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
随机推荐
- Collection接口
Collection接口所定义的方法: clear:清空 retainAll 求一个Collection和另一个 Collection的交集. object[] toArray() 把里面的各个对象 ...
- [转]MySQL 最基本的SQL语法/语句
MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下. DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...
- objccn-图片格式
图像格式存储:位图和矢量图像.位图把值存在阵列中,矢量格式存储的是绘图图像的指令.还有混合格式PostScript能够排布字母甚至位图,使其成为了一个非常灵活的方式.衍生格式pdf. xcdoe6已经 ...
- Javascript获取select下拉框选中的的值
现在有一id=test的下拉框,怎么拿到选中的那个值呢? 分别使用javascript原生的方法和jquery方法 <select id="test" name=" ...
- c#关于类的继承
public class D { public virtual void Run(string name) { Console.WriteLine(name + ",good"); ...
- 浅谈C语言中结构体的初始化
转自:http://www.jb51.net/article/37246.htm <代码大全>建议在变量定义的时候进行初始化,但是很多人,特别是新人对结构体或者结构体数组定义是一般不会初始 ...
- STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解多少钱?
STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解 STM32F105芯片Cortex-M3单片机解密: [凯基迪科技] STM32F105R8解密 | STM32F ...
- tomcat 配置项目指定域名
<Host name="www.xxx.com" appBase="D:/tomcat/webapps/web"> <alias>xxx ...
- [spring源码学习]五-BeanPostProcessor的使用
一.接口描述 spring提供了一个接口类-BeanPostProcessor,我们叫他:bean的加工器,应该是在bean的实例化过程中对bean做一些包装处理,里边提供两个方法 public in ...
- 使用bind方法确定接收者
var buffer = { entries : [], add : function(s){ this.entries.push(s); }, concat : function(){ return ...