// 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> inlinevoid swap(_Ty& _Left, _Ty& _Right){// exchange values stored at _Left and _Rightif (&_Left != &_Right){// different, worth swapping_Ty _Tmp = _Left;_Left = _Right;_Right = _Tmp;}}// TEMPLATE STRUCT pairtemplate<class _Ty1,class _Ty2> struct pair{// store a pair of valuestypedef 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 _Rightif (this != &_Right){// different, worth swappingstd::swap(first, _Right.first);std::swap(second, _Right.second);}}_Ty1 first;// the first stored value_Ty2 second;// the second stored value};// pair TEMPLATE FUNCTIONStemplate<class _Ty1,class _Ty2> inlinebool operator==(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test for pair equalityreturn (_Left.first == _Right.first && _Left.second == _Right.second);}template<class _Ty1,class _Ty2> inlinebool operator!=(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test for pair inequalityreturn (!(_Left == _Right));}template<class _Ty1,class _Ty2> inlinebool operator<(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test if _Left < _Right for pairsreturn (_Left.first < _Right.first ||!(_Right.first < _Left.first) && _Left.second < _Right.second);}template<class _Ty1,class _Ty2> inlinebool operator>(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test if _Left > _Right for pairsreturn (_Right < _Left);}template<class _Ty1,class _Ty2> inlinebool operator<=(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test if _Left <= _Right for pairsreturn (!(_Right < _Left));}template<class _Ty1,class _Ty2> inlinebool operator>=(const pair<_Ty1, _Ty2>& _Left,const pair<_Ty1, _Ty2>& _Right){// test if _Left >= _Right for pairsreturn (!(_Left < _Right));}template<class _Ty1,class _Ty2> inlinepair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2){// return pair composed from argumentsreturn (pair<_Ty1, _Ty2>(_Val1, _Val2));}template<class _Ty1,class _Ty2> inlinevoid swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right){// swap _Left and _Right pairs_Left.swap(_Right);}// TEMPLATE OPERATORSnamespace rel_ops{// nested namespace to hide relational operators from stdtemplate<class _Ty> inlinebool operator!=(const _Ty& _Left, const _Ty& _Right){// test for inequality, in terms of equalityreturn (!(_Left == _Right));}template<class _Ty> inlinebool operator>(const _Ty& _Left, const _Ty& _Right){// test if _Left > _Right, in terms of operator<return (_Right < _Left);}template<class _Ty> inlinebool operator<=(const _Ty& _Left, const _Ty& _Right){// test if _Left <= _Right, in terms of operator<return (!(_Right < _Left));}template<class _Ty> inlinebool 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){}...}