A. Explanation:
pairis a template type that contains two data values, and the two data types can be different. If a function has two return values, if it is the same type, it can be returned with an array, if it is a different type, you can write astruct, but you can use it for convenienceC + +Bring your ownpair, returns apair,with two values. In addition to the application of the return value, when an object has multiple properties, it is common to write astruct, if it is a two attribute, you can usepairto operate. paircan save himself to write astruct.If you have three attributes, you can actually use them.pair, an extreme notation .pair <int, pair<int, int > >The writing is extreme. (Behind the two> >There's a space, or it's going to be>>displacement operator)
Two. Usage:
1. Header files
#include <utility>
2. Defining methods
pair<intstring> A; /* indicates that there are two types in a, the first element is of type int, the second element is of type string, and if it is not initialized when the pair is created, the default constructor is called to initialize it. */Pair<stringstring> A ("James" " Joy "); // Direct Initialization
3. Common operation
(1) for the pair class, since it has only two elements, named First and second, the members can be accessed directly using the ordinary dot operator
pair<string , string > A ( " lily " , Span style= "COLOR: #800000" > " poly " string name;name = Pair.second; /* a.first returns Lilya.second returns Poly */
( 2 ) generate a new pair You can use make_pair Constructs a new < Span lang= "en-US" >pair Type
int 8 ; string " James " ;p Air<intstring>= Make_pair (A, m);
Three. Example
#include <utility>#include<iostream>using namespaceStd;typedef pair<string,string> au;//simplify its claims with typedefintMain () {intFlag; stringx1,x2; Pair<string,string> P1 ("a","BC");//creates a pair object whose two elements are of type string and string, where the first member is initialized to "a" and the second member is initialized to "AB"Au P2 ("a","AA"); Au P3; stringname; Name=p1.second;//returns a data member named second in 1cout<<p1.first<<Endl; cout<<name<<Endl; Flag=p1<P2; cout<<flag<<endl;//determine the size of two pair objects in dictionary orderFlag=p1>P2; cout<<flag<<Endl; while(cin>>x1>>x2) {P3=make_pair (X1,X2);//to generate a new pair objectcout<<p3.first<<"****"<<p3.second<<Endl; } return 0;}
C + + STL pair detailed