关于堆排序
怎么根据优先级排序
2010-08-18 14:56
2010-08-18 15:00
2010-08-19 00:59
2010-08-19 10:24
程序代码:#include <iostream>
#include <queue>
#include <cstdlib>
using namespace std;
struct cnbeta
{
int data , start , time;
friend bool operator< (const struct cnbeta &a,const struct cnbeta &b) //重载<操作符
{
if(a.data == b.data)
if(a.time == b.time)
return a.start > b.start ; //最后比较通话开始时间,按从小到大排
else
return a.time > b.time;//从小到大排通话时间
return a.data > b.data ;//从小到大排日期
}
};
priority_queue <cnbeta> cn;
int main()
{
struct cnbeta tmp;
tmp.data = 1; tmp.time = 2; tmp.start = 3;
cn.push(tmp);
tmp.data = 1; tmp.time = 1; tmp.start = 3;
cn.push(tmp);
tmp.data = 2; tmp.time = 3; tmp.start = 4;
cn.push(tmp);
while(!cn.empty())
{
tmp = cn.top();
cn.pop();
cout << tmp.data << " " << tmp.time << " " << tmp.start << endl;
}
system("pause");
return 0;
}
2010-08-22 00:52
2010-08-22 07:21
2010-08-22 10:51
程序代码:#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct cnbeta
{
int date , start , time;
}cn[100];
int cmp(const struct cnbeta &a , const struct cnbeta &b) //自定义的sort的比较函数
{
if(a.date == b.date)
if(a.time == b.time)
return a.start < b.start; //比较通话开始时间,按从小到大排
else
return a.time < b.time; //从小到大排通话时间
return a.date < b.date; //从小到大排日期
}
int main()
{
int i;
cn[0].date = 1; cn[0].time = 2; cn[0].start = 3;
cn[1].date = 1; cn[1].time = 1; cn[1].start = 3;
cn[2].date = 2; cn[2].time = 3; cn[2].start = 4;
sort(cn,cn+3,cmp); //对数据排序
for (i = 0; i < 3 ; i++)
cout << cn[i].date << " " << cn[i].time << " " << cn[i].start << endl;
system("pause");
return 0;
}
2010-08-23 01:58
2010-08-23 08:55
2010-08-23 22:09