STL Standard Library of C + + learning notes (a) Utility.h header file is a struct template pair

Source: Internet
Author: User

#include <utility>

The pair template class is used to represent two objects as an object.

Use: 1) want the function to return two parameters at the same time; 2) an element that wants to store pairs of values in a container

Pair template class Core code:

#ifndef _utility_
#define _utility_
#include <iosfwd>

Structural body Template Pair

Template<class _ty1,class _ty2> struct pair

{
typedef _ty1 FIRST_TYPE;
typedef _ty2 SECOND_TYPE;

Default constructor

Pair (): First (_ty1 ()), second (_ty2 ())
{
}

Initialize with a specific value, constructor

Pair (const _ty1& _VAL1, const _ty2& _VAL2)
: First (_VAL1), second (_VAL2)
{
}

Copy constructor

Template<class _other1,class _other2>
Pair (const pair<_other1, _other2>& Other)
: First (Other.first), second (Other.second)
{
}

The _ty1 first;//member variable, the first value in the pair, is accessed through the member access operator.
_ty2 second; A member variable, the second value in a pair
};

Template functions and operator overloads for pair

Template<class _ty1,class _ty2> Inline//Reload = =, Judge two pair equal
BOOL operator== (const pair<_ty1, _ty2>& x,const pair<_ty1, _ty2>& y)
{
return (X.first = = Y.first && X.second = = Y.second);
}

Template<class _ty1,class _ty2> Inline//reload! =, judging two pair unequal
BOOL Operator!= (const pair<_ty1, _ty2>& x, const pair<_ty1, _ty2>& y)
{
Return (! ( x = = y));
}

Template<class _ty1,class _ty2> Inline//heavy-duty <, judging two pair size, when judging the size, the first element of higher priority
BOOL operator< (const pair<_ty1, _ty2>& x, const pair<_ty1, _ty2>& y)
{
Return (X.first < Y.first | | ! (Y.first < X.first) && X.second < Y.second);
}

Template<class _ty1,class _ty2> Inline//Heavy duty >, judging two pair size
BOOL Operator> (const pair<_ty1, _ty2>& x, const pair<_ty1, _ty2>& y)
{
Return (y < x);
}

Template<class _ty1,class _ty2> Inline//heavy-duty <=
BOOL Operator<= (const pair<_ty1, _ty2>& x, const pair<_ty1, _ty2>& y)
{
Return (! ( Y < x));
}

Template<class _ty1,class _ty2> Inline//heavy-duty >=
BOOL Operator>= (const pair<_ty1, _ty2>& x, const pair<_ty1, _ty2>& y)
{
Return (! ( x < y));
}

Template<class _ty1,class _ty2> Inline//make_pair template functions, commonly used to generate pair objects, but note that Make_pair parameters cannot have const constants, or they may fail to create
Pair<_ty1, _ty2> Make_pair (_ty1 _val1, _ty2 _val2)
{
Return (Pair<_ty1, _ty2> (_val1, _val2));
}

#endif

Summarize the above code to discover:

1) pair supports three constructors for initialization

2) A pair of values in a pair can be of different data types

3) Two values for pair are accessed via Pair.first and Pair.second, respectively

4) often use Make_pair<class t1,class t2> (T1,T2) to generate new pair objects

5) Pair support size comparison, at this time class T1 and class T2 two classes to be the same or to support the comparison size

Expand: Write a struct template according to the pair format Trio<class _ty1,class _ty2,class _ty3> support for storing three objects of any type

#ifndef _trio_
#define _trio_

#include <iosfwd>

Template <class _ty1,class _ty2,class _ty3> struct TRIO
{
typedef _ty1 FIRST_TYPE;
typedef _ty2 SECOND_TYPE;
typedef _ty3 THIRD_TYPE;

_ty1 first;
_ty2 second;
_ty3 third;

Default constructor
Trio (): First (_ty1 ()), second (_ty2 ()), third (_ty3 ())
{
}

Class with a specific value

Trio (const _ty1& x,const _ty2& y,const _ty3& z): First (x), second (Y), third (z)
{
}

Copy constructor

Template<class _ty1,class _ty2,class _ty3>
Trio (const trio<_ty1,_ty2,_ty3> &other): First (Other.first), second (Other.second), third (Other.third)
{

}
};

operator = = Overload
Template<class _ty1,class _ty2,class _ty3> Inline
BOOL operator = = (const trio<_ty1,_ty2,_ty3>& x, const trio<_ty1,_ty2,_ty3>& y)
{
Return ((X.first = = Y.first) && (X.second = = Y.second) && (X.third = Y.third));
}

Template functions, creating trio objects

Template<class _ty1,class _ty2,class _ty3> Inline
Trio<_ty1,_ty2,_ty3> Make_trio (const _ty1& x,const _ty2& y,const _ty3& z)
{
return trio<_ty1,_ty2,_ty3> (x, y, z);
}

#endif

STL Standard Library of C + + learning notes (a) Utility.h header file is a struct template pair

Related Article

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.