码迷,mamicode.com
首页 > 其他好文 > 详细

STL(2)---<ulitiy>

时间:2015-08-08 20:02:04      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

Copyright (c) 1995 by P.J. Plauger.

下面是ulitiy的一些简单的实现和使用

/**********************************************************************     
* *   Copyright (c)2015,WK Studios   
* *   Filename:  A.h 
* *   Compiler: GCC  vc 6.0    
* *   Author:WK     
* *   Time: 2015 7 8  
* **********************************************************************/

#include <iostream>
using namespace std;

template <class T1,class T2>
struct  Pair
{
      typedef T1 first_type;
	  typedef T2 second_typel;

	  Pair():first(T1()),second(T2()){}
	  Pair(const T1 &v1,const T2 &v2)
		  :first(v1),second(v2)
	  {}

	  T1 first;
	  T2 second;
};


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 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<y);
}
template<class T1,class T2>
inline Pair<T1,T2> make_Pair(const Pair<T1,T2>&x,const Pair<T1,T2>&y)
{
	return Pair<T1,T2>(x,y);
}


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

namespace rei_pos
{
	template<class T> 
		inline bool operator != (const T&x,const T&y)
	{
		return !(x==y);
	}
	template<class T>
		inline bool operator >(const T&x,const T&y)
	{
		return (x>y);
	}
	template<class T>
		inline bool operator<=(const T&x,const T&y)
	{
		return (!(x>y));
	}
	template<class T>
		inline bool operator>=(const T&x,const T&y)
	{
		return (!(x<y));
	}
}
void Require(bool a)
{
	if(!a)
	{
		cout<<"ERRO!\n";
	}
	else
	{
		cout<<"Success\n";
	}
}
int main()
{
  Pair<int,char> pr(3,'a');
  Pair<int,char> p1;
  Require(pr.second=='a');
  Require(p1 != pr);
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

STL(2)---<ulitiy>

标签:

原文地址:http://blog.csdn.net/kai8wei/article/details/47360805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!