还是nyist 8(一种排序)

呵呵~还是回到这个问题上,想着开始看STL时,想到unique_copy中的删除,正好符合题目要求,但是实际操作过程中却遇到了问题,下面是一个错误的代码:

#include<iostream> 
#include<vector>
#include<numeric>
#include<iterator> 
using namespace std; 
int a,b,n;
class Rect {   
public:
    Rect(){num=length=width=0;};
    Rect(int n,int l,int w):num(n),length(l),width(w){} 
    friend bool operator<(const Rect&,const Rect&);
    friend istream& operator>>(istream&,Rect&);
    friend ostream& operator<<(ostream&,const Rect&);   
private:
    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; 
    cin>>a>>b; 
    r.length=(a>b?a:b); 
    r.width=(a<b?a:b); 
} 
ostream& operator<<(ostream& out,const Rect& r) 
{   return out<<r.num<<" "<<r.length<<" "<<r.width; 
} 
int main() 
{   int k; 
    cin>>k; 
    while(k--) 
    {  vector<Rect> v,v1;
       istream_iterator<Rect> input(cin);
       ostream_iterator<Rect> output(cout,"\n");
       Rect r;     
       cin>>n;    
       while(n--) 
       {  cin>>r; 
          v.push_back(r); 
       }     
       sort(v.begin(),v.end()); 
       unique_copy(v.begin(),v.end(),output); //调用后出现的问题我会贴在后面我看不懂~~呵呵,
    } 
    return 0; 
}

这是调用后的代码出现的问题~~

      typedef typename iterator_traits<_OutputIterator>::iterator_category
	_IterType;

      if (__first == __last) return __result;
      return std::__unique_copy(__first, __last, __result, _IterType());

想了很久,百度了下,说是参数不匹配吧,不懂~~呵呵,昨天晚上想了很久,没有想出来,睡了一觉,突然想到我没有定义Rect中判定两个成员是否相等的函数,所以用unique_copy时由于是Rect类的,它自己本身并不知道如何才是相等,所以我得做个运算符重载函数来判定相等,等晚上回来了按着自己的想法重新做了下,呵呵,果然没有想错,所以注意了:在调用STL标准函数库中的某些函数时,如果是自己定义的类,得重新自己做函数给出判定,比方说用sort函数时给出int时他可以自己判定,但要是你给出自己定义的类作为模板库中的成员,他就不一定能够判定了,所以你得重新给出一个判定函数要sort根据你的判定函数来排序~~~下面给出正确代码,题目很简单,但方法不同,这个题目我前面的博文中有解答,这里只是发下unique_copy的做法:

#include<iostream> 
#include<vector>
#include<numeric>
#include<algorithm>
#include<iterator> 
using namespace std; 
int a,b,n;
class Rect {   
public:
    Rect(){num=length=width=0;};
    Rect(int n,int l,int w):num(n),length(l),width(w){} 
    friend bool operator<(const Rect&,const Rect&);
    bool operator==(const Rect&);
    friend istream& operator>>(istream&,Rect&);
    friend ostream& operator<<(ostream&,const Rect&);   
private:
    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); 
} 
inline bool Rect::operator==(const Rect& r)
{   return num==r.num&&length==r.length&&width==r.width;
}
istream& operator>>(istream& in,Rect& r) 
{   in>>r.num; 
    cin>>a>>b; 
    r.length=(a>b?a:b); 
    r.width=(a<b?a:b); 
} 
ostream& operator<<(ostream& out,const Rect& r) 
{   return out<<r.num<<" "<<r.length<<" "<<r.width; 
} 
int main() 
{   int k; 
    cin>>k; 
    while(k--) 
    {  vector<Rect> v,v1;
       //istream_iterator<Rect> input(cin);
       ostream_iterator<Rect> output(cout,"\n");
       Rect r;     
       cin>>n;    
       while(n--) 
       {  cin>>r; 
          v.push_back(r); 
       }     
       sort(v.begin(),v.end()); 
       unique_copy(v.begin(),v.end(),back_inserter(v1));
       copy(v1.begin(),v1.end(),output); 
    } 
    return 0; 
}

还有个就是我对那个const的用法还不是很熟悉,得加油了~~~


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值