nyist 8 set的应用

时间:2014-08-10 21:06:10   收藏:0   阅读:239

bubuko.com,布布扣

 

bubuko.com,布布扣

 

 

bubuko.com,布布扣

 

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;


class rect //类 声明公有
{ public:
int bh,a,b;
bool operator<(const rect & x) const
{
if(x.bh!=bh) return bh<x.bh ;
if(x.a!=a) return a<x.a ;
if(x.b!=b) return b<x.b ;
return 0;
}
};

 

 

 

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;


struct rect //结构体 公有 ?
{ // ?
int bh,a,b; // ?
bool operator<(const rect & x) const // 运算符重载 小于号
{
if(x.bh!=bh) return bh<x.bh ;
if(x.a!=a) return a<x.a ;
if(x.b!=b) return b<x.b ;
return 0; // 三者相等
}
};

int main()
{
int m,n,i;
struct rect t;
set <rect>my;
set <rect>::iterator it ;
cin>>n ;
while(n--)
{
cin >>m;
while(m--)
{
cin>>t.bh>>t.a>>t.b ;
if(t.a< t.b) swap(t.a,t.b);
my.insert(t); // 插入到 set “集合”
}
for(it=my.begin();it!=my.end();it++)
cout<<(*it).bh<<" "<<(*it).a<<" " <<(*it).b<<endl ;
my.clear();

}
}

 

 


***************************************************************************************************************

 

 

#include<iostream>
#include<set>
#include<iterator>
using namespace std;
struct Rect
{
int num,length,width;
};
bool operator<(const Rect& r1,const Rect& r2)
{

return r1.num<r2.num || r1.num==r2.num && r1.length<r2.length ||r1.num==r2.num&&r1.length==r2.length &&r1.width<r2.width;
}
istream& operator>>(istream& in,Rect& r)
{

in>>r.num;
int a,b;
cin>>a>>b;
r.length=max(a,b);

r.width=min(a,b);

return in;
}
ostream& operator<<(ostream& out,const Rect& r)
{
return out<<r.num<<" "<<r.length<<" "<<r.width;
}
int main()
{

int num;

cin>>num;

while(num--)

{

set<Rect> rs;

Rect r;

int n;

cin>>n;

while(n--)

{

cin>>r;
rs.insert(r);
}

copy(rs.begin(),rs.end(),ostream_iterator<Rect>(cout,"\n"));

}
}

 

 

bubuko.com,布布扣

 

 

 

 

 

bubuko.com,布布扣

 

 

 

bubuko.com,布布扣

 

极目,远眺(752284118) 15:55:13
nyist 8一种排序: 用set自定义排序规则即可
#include <iostream>
#include <set>
using namespace std;
struct ju
{
int id,x,y;
bool operator <(const ju &a)const
{
if(id==a.id)
{
if(x==a.x) return y<a.y;
else return x<a.x;
}
else return id<a.id;
}
}tt;
set<ju> my;
set<ju> ::iterator it;
int main(int argc, char *argv[])
{
int t,n,i,j;
cin>>t;
while(t--)
{
cin>>n; my.clear();
for(i=0;i<n;i++)
{
cin>>tt.id>>tt.x>>tt.y;
if(tt.x<tt.y) swap(tt.x,tt.y);
my.insert(tt);
}
for(it=my.begin();it!=my.end();it++)
cout<<(*it).id<<" "<<(*it).x<<" "<<(*it).y<<endl;
}
return 0;
}

nyist 8 set的应用,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!