utility

来源:互联网 发布:网络原创歌手 编辑:程序博客网 时间:2024/05/05 23:49

 #ifndef UTILITY_
#define UTILTIY_
#include <iosfwd>
namespace std
{
 //TEMPLATE STURCT pair
 template <class T1, class T2> struct pair
 {
  typedef T1 first_type;
  typedef T2 second_type;
  pair() : first(T1()), second(T2()) {}
  pair(const T1& V1, const T2& V2) : first(V1), second(V2) {}
  template<class U1, class U2> pair(const pair<U1, U2>& X )
   : first(X.first), second(X.second) {}
  T1 first;
  T2 second;
 };

 //pair template operations
 template<class T1, class T2> inline
  bool operator==(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return ( (X.first == Y.first) && (X.second == Y.second) );
 }
 
 template<class T1, class T2> inline
  bool operator!=(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return(!(X == Y));
 }

 template<class T1, class T2> inline
  bool operator<(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return( (X.first < Y.first) || !(Y.first < X.first) && (X.second < Y.second) );
 }

 template<class T1, class T2> inline
  bool operator>(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return(Y < X);
 }

 template<class T1, class T1> inline
  bool operator<=(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return( !(Y < X) );
 }

 template<class T1, class T1> inline
  bool operator>=(const pair<T1, T2>& X, const pair<T1, T2>& Y)
 {
  return( !(X < Y));
 }

 template<class T1, class T1> inline
  pair<T1, T2> make_pair(cosnt T1& X, const T2& Y)
 {
  return pair<T1, T2>(X, Y);
 }

 //template rel_ops
 namespace rel_ops
 {
  template<class T> inline
   bool operator!=(cosnt T& X, const T& Y)
  {
   return ( !(X == Y) );
  }

  template<class T> inline
   bool operator>(cosnt T& X, const T& Y)
  {
   return ( Y < X );
  }

  template<class T> inline
   bool operator<=(cosnt T& X, const T& Y)
  {
   return ( !(Y < X) );
  }

  template<class T> inline
   bool operator>=(cosnt T& X, const T& Y)
  {
   return ( !(X < Y) );
  }
 }
}/*namespace std*/
#endif /*UTILITY_*/

 

#include "stdafx.h"
//test utiltiy
#include <assert.h>
#include <iostream>
#include <utility>
using namespace std;

typedef pair<int, char> Pair_ic;
Pair_ic p0;

class Int
{
public:
 Int(int v) : val(v) {}
 bool operator==(Int x) const
 {
  return (val == x.val);
 }
 bool operator<(Int x) const
 {
  return (val < x.val);
 }
private:
 int val;
};

int main()
{
 Pair_ic p1 = p0, p2(2,'a');

 //Test pair
 assert(p1.first == 0);
 assert(p1.second ==0);
 assert(p2.first == 3);
 assert(p1.second == 'a');
 assert(p2 == make_pair((Pair_ic::first_type)3, (Pair_ic::second_type)'a'));
 assert(p2 < make_pair((Pair_ic::first_type)4, (Pair_ic::second_type)'a'));
 assert(p2 < make_pair((Pair_ic::first_type)3, (Pair_ic::second_type)'b'));
 assert( p1 != p2 );
 assert( p2 > p1 );
 assert( p2 <= p1 );
 assert( p2 >= p1 );

 using namespace std::rel_ops;
 Int a(2),b(3);
 assert(a == a);
 assert(a < b);
 assert(a != b);
 assert(b > a);
 assert(a <= b);
 assert(b >= a);
 cout<<"Success testing <utility>"<<endl;
 return 0;


}