A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/std-make_pair-in-cpp/ below:

make_pair() in C++ STL - GeeksforGeeks

make_pair() in C++ STL

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.

make_pair(key, val);

Parameters Return Value Examples of make_pair()

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;
}

Output
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