Never or now.
本文从WordPress迁移而来, 查看全部WordPress迁移文章
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;struct student{ int a,b,c; student(){} student(int aa,int bb,int cc):a(aa),b(bb),c(cc){} void show(){ printf("%d %d %d\n",a,b,c); } //重载<运算符,对应less算子,当使用less算子时才生效 bool operator < (const student &temp)const{ //习惯上,比较的符号和重载的运算符一样,即 a < temp.a 和 < 一样 return a < temp.a; //less对应的是大堆,这样写的结果是按照a从大到小出队 } //重载>运算符,对应greater算子,当使用greater算子时才生效 bool operator > (const student &temp)const{ //习惯上,比较的符号和重载的运算符一样,即 a > temp.a 和 > 一样 return a > temp.a; //greater对应的是小堆,这样写的结果是按照a从小到大出队 }};struct cmp{ //自定义比较算子,是一个struct或者class bool operator () (const student &x , const student &y)const{ //重载的运算符为() return x.a > y.a; }};priority_queue<int> pq1; //默认类型intpriority_queue<student> pq2; //没有说明使用什么比较算子,默认使用lesspriority_queue< student , vector<student> , less<student> > __pq2; //声明使用less算子,必须重载<运算符,因为student是自定义类型priority_queue< student , vector<student> , greater<student> > pq2__; //声明使用greater算子,必须重载>运算符,因为student是自定义类型priority_queue< student , vector<student> , cmp > __pq2__; //使用完全自定义的比较算子priority_queue< student , vector<student> > PQ2;//不写第3个参数/*PQ的模板声明带有3个参数priority_queue<Type,Container,Functional>Type为数据类型Container为保存数据的容器Functional为元素的比较方式(比较算子)# Container必须是用数组实现的容器,比如vector,dequeue但不能用list关于参数1.PQ的第一个参数是必须的,即student2.后面的两个参数可以不写,不写的话,第2个参数默认vector,第3个参数默认是less3.当然,你可以写了第1,第2个参数,第3个参数不写,默认使用less4.但是!你不能写了第1个,第3个参数,却不写第2个参数!5.全部都写是一个好习惯6.当这个PQ比较简单的时候,只写第1个参数是个不错的选择*/int main(){ //插入数据来测试吧 return 0;}