我使用这个摸板类的程序
    1. #include <iostream>
    2. #include <utility>
    3. using namespace std;
    4. int main(){ 
    5.    pair<int,int> a;
    6.    a=make_pair(3,5);
    7.    pair<int,int> b(3,8);
    8.    pair<int,int> c(b);
    9.    cout<<a.first<<"   "<<a.second<<endl;
    10.    cout<<b.first<<"   "<<b.second<<endl;
    11.    cout<<c.first<<"   "<<c.second<<endl;
    12.    cout<<(a==b)<<endl;
    13.    cout<<(a!=b)<<endl;
    14.    cout<<(a<b)<<endl;
    15.    return 0;
    16.    
  1. //utility standard header
  2. #ifndef _UTILITY_
  3. #define _UTILITY_
  4. #include <iosfwd>
  5. namespace std {
  6.     //TEMPLATE STRUCT pair
  7.     template<class T1, class T2> 
  8.     struct pair  //其实与类是一样的,只不过默认访问权限是public
  9.     {
  10.         typedef T1 first_type;
  11.         typedef T2 second_type;
  12.         
  13.         pair():first(T1()),second(T2())//默认构造函数
  14.             {
  15.             }
  16.         
  17.         pair(const T1& V1,const T2 &V2)
  18.         : first(V1),second(V2)
  19.             {
  20.             }
  21.             
  22.             templateclass U1, class U2>//拷贝构造函数
  23.         pair(const pair<U1, U2> & x)
  24.         :first(X.first) ,second(X.second)
  25.             {
  26.             }
  27.             T1 first;
  28.             T2 second;
  29.     };
  30.     
  31.     //  pair TEMPLATE OPERATORS
  32.     //注意operator==,operator<是实现其他的关键
  33.     template<class T1, class T2>
  34.     inline bool operator==(const pair<T1,T2>& X,
  35.                                                  const pair<T1,T2>& Y)
  36.     {
  37.         return (X.first==Y.first && X.second ==Y.second);
  38.     }
  39.     
  40.     temlate<class T1, class T2>
  41.     inline bool operator!=(const pair<T1,T2>& X,
  42.                                                  const pair<T1,T2>& Y)
  43.     {
  44.         return (!(X==Y));
  45.     }
  46.     
  47.     temlate<class T1, class T2>
  48.     inline bool operator<(const pair<T1,T2>& X,
  49.                                                  const pair<T1,T2>& Y)
  50.     {
  51.         return  ( X.first <Y.first ||
  52.                  (!(Y.first<X.first) && X.second<Y.second ));
  53.     }
  54.     
  55.     temlate<class T1, class T2>
  56.     inline bool operator>(const pair<T1,T2>& X,
  57.                                                  const pair<T1,T2>& Y)
  58.     {
  59.         return  (Y<X);
  60.     }
  61.     
  62.     temlate<class T1, class T2>
  63.     inline bool operator<=(const pair<T1,T2>& X,
  64.                                                  const pair<T1,T2>& Y)
  65.     {
  66.         return  (!(Y<X));
  67.     }
  68.     
  69.     temlate<class T1, class T2>
  70.     inline bool operator>=(const pair<T1,T2>& X,
  71.                                                  const pair<T1,T2>& Y)
  72.     {
  73.         return  (!( X<Y));
  74.     }
  75.     
  76.     template<class T1, class T2>  
  77.     //注意 const修饰符不起作用
  78.     inline pair<T1, T2> make_pair(const T1& X,const T2& Y)
  79.     {
  80.         return ( pair<T1, T2> (X, Y) );
  81.     }
  82.            //TEMPLATE OPERATORS
  83.       namespace rel_ops
  84.       {
  85.         template<class T>
  86.         inline bool operator!=(const T& X, cosnt T& Y)
  87.         {
  88.             return (!(X==Y) );
  89.         }
  90.         
  91.         template<class T>
  92.         inline bool operator>(const T& X, cosnt T& Y)
  93.         {
  94.             return (Y<X);
  95.         }
  96.         
  97.         template<class T>
  98.         inline bool operator<=(const T& X, cosnt T& Y)
  99.         {
  100.             return (!(Y<X) );
  101.         }
  102.         
  103.         template<class T>
  104.         inline bool operator>=(const T& X, cosnt T& Y)
  105.         {
  106.             return (!(X<Y) );
  107.         }
  108.         
  109.     }      
  110. }/* namespace std*/
  111. #endif /*UTILITY*/

测试这个类的文件

  1. //test <utility>
  2. #include <assert.h>
  3. #include <iostream>
  4. #include "utility.h"
  5. using namespace std;
  6. typedef pair<intchar> Pair_ic;
  7. Pair_ic p0;
  8. class Int
  9. {
  10.     public:
  11.         Int(int v):val(v)
  12.         {
  13.         }
  14.         
  15.         bool operator==(Int x) const
  16.         {
  17.             return (val==x.val);
  18.         }
  19.         bool operator<(Int x) const
  20.         {
  21.             return (val<x.val);
  22.         }
  23.         
  24.         private:
  25.             int val;
  26. };
  27. //TEST <utility>
  28. int main()
  29. {
  30.     Pair_ic p1=p0;
  31.     Pair_ic p2(3,'a');
  32.     
  33.     //TEST pair
  34.     assert(p1.first==0);
  35.     assert(p1.second==0);
  36.     assert(p2.first==3);
  37.     assert(p2.second=='a');
  38.     assert(p2==make_pair( ( Pair_ic::first_type) 3,
  39.                                                 ( Pair_ic::second_type) 'a') );
  40.     assert(p2 < make_pair( (Pair_ic::first_type) 4,
  41.                              (Pair_ic::second_type) 'b') );
  42.   assert(p2 < make_pair( (Pair_ic::first_type) 3,
  43.                          (Pair_ic::second_type) 'b') );
  44.   assert(p1 != p2);
  45.   assert(p2 > p1);
  46.   assert(p2 <=p2);
  47.   assert(p2>=p2);
  48.    //TEST rel_ops
  49.    using namespace std::rel_ops;
  50.     Int a(2), b(3);
  51.     assert(a==a);
  52.    //   assert(a<b);
  53.     assert(a != b);
  54.     assert(b > a);
  55.     assert(a <= b);
  56.     assert(b >= a);
  57.     cout<<"Success testing <utility>"<<endl;
  58.     return 0;
  59.                                                                                                             
  60. }

 

 

你可能感兴趣的:(类)