The make_pair()
function, which comes under the Standard Template Library of C++, is mainly used to construct a pair
object with two elements.
In other words, it is a function that creates a value pair without writing the types explicitly.
To use the make_pair()
function, you will need to include the <utility>
header file in your program, as shown below:
#include <utility>
The syntax of the make_pair()
function is shown below:
make_pair(value1,value2)
The make_pair()
function accepts the following two parameters:
value1
: The first value to construct the pair from.value2
: The second value to construct the pair from.The make_pair()
function returns a pair
object that contains the two elements, value1
and value2
.
The code below shows how you can use the make_pair()
function in C++:
#include <iostream>#include <utility>using namespace std;int main() {pair<int, string> p;p= make_pair(44, "Edpresso");cout << "Pair created successfully" << endl;cout << "Pair's First Value: " << p.first << endl;cout << "Pair's Second Value: " << p.second;return 0;}
The code snippet above performs the following actions:
main()
function is initialized.pair
object is initialized, whose first value is of type int
and the second value of type string
.make_pair()
function is invoked with the required values as its parameters. The function returns the newly created pair
object.pair
object values are printed.