使用优先队列保存一组矩形对象,矩形长宽为1-100的随机数,要求优先队列的元素权重为矩形面积,并按照面积大小依次输出矩形的长和宽。
strcut square{
int length;
int width;
square(int l,int w):lenth(l),width(w){}
};
strcut compare{
bool operator()(square x,square y){
return((x.length*x.length)<(y.length*y.length));}
};
int main(){
priority_queue<square,vector<square>,compare> p;
for(int i=0;i<10;i++)
{p.push(square(rand()%100+1,eand()%100+1));}
while(!p.empty())
{cout<<p.top().length<<"*"<<p.top().width<<endl;
p.pop();}
getchar();
}