Last Updated : 23 Jul, 2025
In C++, make_pair() is a standard library function 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. In this article, we will learn about make_pair() function in C++.
Let’s take a quick look at a simple example that illustrates std::make_pair():
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating a pair
auto p = make_pair('A', 11);
cout << p.first << " " << p.second;
return 0;
}
This article covers the syntax, usage, and common examples of make_pair() function in C++:
Syntax of make_pair()The make_pair() function defined inside <utility> header file.
Parametersmake_pair(key, val);
The following examples demonstrates the use of make_pair() function in different scenarios and for different purposes:
Creating a Pair of Integers C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating a pair of integers
pair<int, int> p = make_pair(10, 20);
cout << p.first << ": " << p.second;
return 0;
}
Creating Pair of Integer and String C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Make pair of integer and string
auto p1 = make_pair(1, "Geeks");
cout << p1.first << ": " << p1.second;
return 0;
}
Inserting Pairs in a Map C++
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> m;
// Inserting pairs into the map
m.insert(make_pair(1, "Geeks"));
m.insert(make_pair(2, "Gfg"));
for (const auto& pair : m) {
cout << pair.first << ": " << pair.second
<< endl;
}
return 0;
}
Alice Geeks Bob Geeks for Geeks
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4