基础法门
对基本数据类型的操作
#include <iostream>
#include <queue>
using namespace std;
int main(void)
{
priority_queue<int> que;
que.push(2);
que.push(3);
que.push(3);
que.push(4);
cout<<que.top()<<endl;
que.pop();
que.pop();
cout<<que.size()<<endl;
que.pop();
que.pop();
if(que.empty())cout<<"Is empty"<<endl;
priority_queue<int,vector<int>,greater<int> > vi;
vi.push(2);
vi.push(6);
vi.push(4);
for(int i = vi.size();i > 0;i--){
cout<<vi.top()<<endl;
vi.pop();
}
return 0;
}
对结构体的操作
#include <iostream>
#include <queue>
using namespace std;
struct fruit{
int price;
string name;
friend operator < (fruit f1,fruit f2){
return f1.price < f2.price;
}
};
int main(void)
{
priority_queue<fruit> vi;
fruit f1;
f1.name = "apple";
f1.price = 2;
fruit f2;
f2.name = "orange";
f2.price = 3;
fruit f3;
f3.name = "potato";
f3.price = 1;
vi.push(f1);
vi.push(f2);
vi.push(f3);
for(int i = vi.size();i > 0;i--){
cout<<vi.top().name<<' '<<vi.top().price<<endl;
vi.pop();
}
return 0;
}
struct gal{
int price;
string name;
friend operator < (gal f1,gal f2){
return f1.price > f2.price;
}
};