pair make_pair function


The make_pair function of pair template allows you to make a pair template object out of two data.

template<typename T1, typename T2>
constexpr pair<typename T1, typename T2 > make_pair( T1&& x , T2&& y);

Parameters:
T1 – The first data of the pair object.

T2 – The second data of the pair object.

Return type
pair :The pair object made out of the two datas passed as
the arguments.

Note,the data passed as the arguments must match with the template argument type of the pair object.


 


Stop wasting time,earn money($$$) from your website-Join Now!

Here are some of the implementations of make_pair function.

Code example

pair<int,string> pr{ 345 ,”New string” } , pr1 , pr2 , pr3 ;

pr1=pr ; ///wok fine

pr2=make_pair( 34 , “make_pair” ); ///work fine

pr3={ 90 , “implicit” }; ///work fine

Even without the make_pair function sirect assignment of the initializer to
the pair object is allowed.Since the pair structure allows implicit conversion
from initializer to the pair type.

Here is another code example.

Code example

pair<string,string> func( )
{
return make_pair( “Batman” , “Superman”);
}

int main( )
{
pair<string,string>pr=func( ) ;

string s=pr.first + ” Vs ” + pr.second;

cout<< s ;

cin.get( )
return ;
}

Output

Batman Vs Superman

The program works fine(the movie wasn’t a hit though).