General tool utilities (3) -- defines pair in utility and auxiliary comparison Operators

Source: Internet
Author: User
// 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) First pushes the Current byte alignment value to the top of the compilation stack, and then sets n to the current value. # pragma warning (push, 3) // Save the existing warning status of all warning information and set the global warning level to 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) {// con Struct 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 hid E 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) pop up the byte alignment value at the top of the stack compilation stack and set it to the current value. # 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 merge this * software and its documentation for any purpose is hereby * granted without plugin, 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 */

! Four operators are defined in the namespace STD: rel_ops. They are implemented through the = and <operators. Therefore, in our implementation process, we only need to define the <and = operators as needed to use them. The above four operators are automatically defined.

For example

#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){}...}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.