通用工具Utilities(三)----定义于utility中的pair和辅助性的比较操作符

// utility standard header
#pragma once
#ifndef _UTILITY_
#define _UTILITY_
#ifndef RC_INVOKED
#include <iosfwd>

#ifdef _MSC_VER
 #pragma pack(push,_CRT_PACKING)//#pragma pack(push, n)先将当前字节对齐值压入编译栈栈顶, 然后再将 n 设为当前值.
 #pragma warning(push,3)//保存所有警告信息的现有的警告状态,并且把全局警告等级设定为n。   
#endif  /* _MSC_VER */

_STD_BEGIN
		// TEMPLATE FUNCTION swap (from <algorithm>)
template<class _Ty> inline
	void swap(_Ty& _Left, _Ty& _Right)
	{	// exchange values stored at _Left and _Right
	if (&_Left != &_Right)
		{	// different, worth swapping
		_Ty _Tmp = _Left;

		_Left = _Right;
		_Right = _Tmp;
		}
	}

		// TEMPLATE STRUCT pair
template<class _Ty1,class _Ty2> 
	struct pair
	{	// store a pair of values
	typedef pair<_Ty1, _Ty2> _Myt;
	typedef _Ty1 first_type;
	typedef _Ty2 second_type;

	pair()
		: first(_Ty1()), second(_Ty2())
		{	// construct from defaults
		}

	pair(const _Ty1& _Val1, const _Ty2& _Val2)
		: first(_Val1), second(_Val2)
		{	// construct from specified values
		}

	template<class _Other1,class _Other2>
		pair(const pair<_Other1, _Other2>& _Right)
		: first(_Right.first), second(_Right.second)
		{	// construct from compatible pair
		}

	void swap(_Myt& _Right)
		{	// exchange contents with _Right
		if (this != &_Right)
			{	// different, worth swapping
			std::swap(first, _Right.first);
			std::swap(second, _Right.second);
			}
		}

	_Ty1 first;	// the first stored value
	_Ty2 second;	// the second stored value
	};

		// pair TEMPLATE FUNCTIONS
template<class _Ty1,class _Ty2> inline
	bool operator==(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair equality
	return (_Left.first == _Right.first && _Left.second == _Right.second);
	}

template<class _Ty1,class _Ty2> inline
	bool operator!=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair inequality
	return (!(_Left == _Right));
	}

template<class _Ty1,class _Ty2> inline
	bool operator<(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left < _Right for pairs
	return (_Left.first < _Right.first ||
		!(_Right.first < _Left.first) && _Left.second < _Right.second);
	}

template<class _Ty1,class _Ty2> inline
	bool operator>(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left > _Right for pairs
	return (_Right < _Left);
	}

template<class _Ty1,class _Ty2> inline
	bool operator<=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left <= _Right for pairs
	return (!(_Right < _Left));
	}

template<class _Ty1,class _Ty2> inline
	bool operator>=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left >= _Right for pairs
	return (!(_Left < _Right));
	}

template<class _Ty1,class _Ty2> inline
	pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
	{	// return pair composed from arguments
	return (pair<_Ty1, _Ty2>(_Val1, _Val2));
	}

template<class _Ty1,class _Ty2> inline
	void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
	{	// swap _Left and _Right pairs
	_Left.swap(_Right);
	}

		// TEMPLATE OPERATORS
	namespace rel_ops
		{	// nested namespace to hide relational operators from std
		template<class _Ty> inline
			bool  operator!=(const _Ty& _Left, const _Ty& _Right)
			{	// test for inequality, in terms of equality
			return (!(_Left == _Right));
			}

		template<class _Ty> inline
			bool operator>(const _Ty& _Left, const _Ty& _Right)
			{	// test if _Left > _Right, in terms of operator<
			return (_Right < _Left);
			}

		template<class _Ty> inline
			bool operator<=(const _Ty& _Left, const _Ty& _Right)
			{	// test if _Left <= _Right, in terms of operator<
			return (!(_Right < _Left));
			}

		template<class _Ty> inline
			bool operator>=(const _Ty& _Left, const _Ty& _Right)
			{	// test if _Left >= _Right, in terms of operator<
			return (!(_Left < _Right));
			}
		}
_STD_END
#ifdef _MSC_VER
 #pragma warning(pop)
 #pragma pack(pop)//#pragma pack(pop)将编译栈栈顶的字节对齐值弹出并设为当前值.
#endif  /* _MSC_VER */

#endif /* RC_INVOKED */
#endif /* _UTILITY_ */


/*
 * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 */

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright notice
 * appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 V5.02:0009 */


!= , > , <= , >= 4个操作符定义于命名空间std::rel_ops中。它们的实现都是通过==和<运算符。因此在我们实现过程中根据情况只需定义 < 和 == 操作符就可以使用它们, 以上4种操作符自动获得定义。

举例来说

#include <utility>
class X{
	public:
	bool operator == (const T& x) const;
	bool operator < (const T& x) const;
	...
};

void fun()
{
	using namespace std::rel_ops;
	X x1,x2;
	if(x1 != x2){}
	...
	if(x1 > x2){}
	...
}



你可能感兴趣的:(通用工具Utilities(三)----定义于utility中的pair和辅助性的比较操作符)