std::make_pair() in C++
Last Updated :
20 Mar, 2024
In C++, std::make_pair() is a standard library function that is used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. It is defined as a function template inside the <utility> header file.
Syntax of std:make_pair()
std::make_pair(key, value);
Parameters of make_pair()
- key: Represents the key for the pair object i.e. first value.
- value: Represents the value for the pair object i.e. second value.
Return Value of make_pair()
The make_pair() function returns an object of std::pair having first and second elements as key and value passed as argument.
Example of make_pair()
C++
// C++ program to illustrate
// std::make_pair() function in C++
#include <iostream>
#include <utility>
using namespace std;
int main()
{
// Pair Declared
pair<int, string> p1;
// Pair Initialized using make_pair()
p1 = make_pair(1, "GeeksforGeeks");
// using it with auto type deduction
auto p2 = make_pair("GeeksforGeeks", 1);
// Pair Printed
cout << "Pair 1: " << p1.first << ", " << p1.second
<< endl;
cout << "Pair 2: " << p2.first << ", " << p2.second;
return 0;
}
OutputPair 1: 1, GeeksforGeeks
Pair 2: GeeksforGeeks, 1
Share your thoughts in the comments
Please Login to comment...