c++中set放结构体,默认插入排好序
#include<iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
typedef struct rect
{
int id;
int height;
int width;
bool operator < (const rect &rhs) const //set中放结构体必不可少
{
if(rhs.id!=id) //从小到大排序
{
return rhs.id>id;
}
else
{
if(rhs.width!=width)
{
return rhs.width>width;
}
else
{
return rhs.height>height;
}
}
}
}RECT;
int iscan()
{
return 1;
}
int main()
{
int ncase;
int nlines;
set<RECT> result;
cin>>ncase;
while(ncase--)
{
result.clear();
cin>>nlines;
for(int i=0;i<nlines;i++)
{
int id,data1,data2;
cin>>id>>data1>>data2;
RECT r1;
r1.id=id;
r1.height=min(data1,data2);
r1.width=max(data1,data2);
result.insert(r1);
}
if(!iscan()) {
}
else{
//sort(result.begin(),result.end(),mysort);
set<RECT>::iterator it;
for(it=result.begin();it!=result.end();it++)
{
cout<<it->id<<" "<<it->width<<" "<<it->height<<endl;
}
}
}
// system("pause");
return 0;
}