Pair stores two data in a template way
Namespace Std {
Template <typename T1, TypeName t2>
struct Pair {
Member
T1 first;
T2 second;
...
};
}
P.first
P.second
Get<0> (P) c++11
Get<1> (P) c++11
Example Pairprinttest ()
//=======================================
Pair accepts multiple parameters
Example Pairtuple ()
//========================================
Use ref () instead of a reference to a parameter in Make_pair &
Example Pairreftest
//=============================================
The use of the tie
Example Tietest
//=====================================
#include <tuple>#include<iostream>#include<utility>#include<tuple>#include<complex>#include<string>using namespacestd;template<typename T1,typename t2>ostream&operator<< (ostream&STRM,ConstPAIR<T1, t2>&p) { returnSTRM <<"["<< P.first <<","<< P.second <<"]";}voidPairprinttest () {typedef std::p Air<int,float>Intfloatpair; Intfloatpair P ( the,3.14f); cout<<Get<0> (P) <<" "<<Get<1> (P) <<Endl; cout<< P <<Endl;}//==============================classFoo { Public: Foo (tuple<int,float>) {cout<<"Foo::foo (tuple)"<<Endl; } template<typename ... Args>Foo (args ... args) {cout<<"Foo::foo (args ...)"<<Endl; }};voidpairtupletest () {tuple<int,float> t (1,2.22f); //Pass the tuple as a whole to the constructor Offoo:pair<int, foo> p1 ( the, T); //Pass the elements ofthe tuple to the constructor Offoo:pair<int, foo> p2 (piecewise_construct, Make_tuple ( the), t);}voidpairreftest () {inti =0; Auto P= Make_pair (&i, &i); ++P.first; ++P.second; cout<<"When use ' & ' I:"<< I <<Endl; Auto P1= Make_pair (ref(i),ref(i)); ++P1.first; ++P1.second; cout<<"When use ' ref () ' I:"<< I <<Endl;}voidTietest () {pair<Char,Char> P = Make_pair ('x','y');//pair Oftwo chars CharC; Tie (Ignore, c)= P;//extract second value into C (Ignore first one)cout << C <<Endl; Tie (c, ignore)=p; cout<< C <<Endl;}voidturpletest () {intn =998; Auto TT= Std::tuple_cat (Std::make_tuple ( the,7.7,"Hello"), Std::tie (n));}int_tmain (intARGC, _tchar*argv[]) {pairprinttest (); Pairtupletest (); Pairreftest (); Tietest (); Turpletest (); return 0;}
C++11 STL Learning Pair