/× 自定义结构 ×/
#include <iostream>
#include <queue>
using namespace std;
struct stata
{
int a, b;
stata(int aa=0, int bb=0):a(aa),b(bb)
{}
};
bool operator<(const stata x, const stata y)
{
return x.a < y.a;
}
struct cmp
{
bool operator()(stata x, stata y)
{
return x.a > y.a;
}
};
int main()
{
priority_queue <stata> big; //最大优先
priority_queue <stata, vector<stata>, cmp> small; //最小优先
int aa[10], bb[10];
for(int i =0; i < 10; i++)
{
aa[i] = rand() % 20;
bb[i] = rand() % 20;
big.push(stata(aa[i], bb[i]));
small.push(stata(aa[i], bb[i]));
}
cout << "from big to small:" << endl;
while(!big.empty())
{
cout << big.top().a << ' ' << big.top().b << endl;
big.pop();
}
cout << endl;
cout << "from small to big:" << endl;
while(!small.empty())
{
cout << small.top().a << ' ' << small.top().b << endl;
small.pop();
}
cout << endl;
return 0;
}
|