Javatpoint Logo
Javatpoint Logo

Make_pair in C++

C++ is a powerful programming language that provides a wide range of tools and capabilities to assist programmers in creating streamlined code. The C++ Standard Library's function template for making pairs quickly is std::make_pair(), which is one of these tools. In this article, we will go into detail to demonstrate the use of std::make_pair(), including its syntax, use cases, and examples with the right output. The C++ Standard Library's <utility> header has a definition for the function template std::make_pair(). Its main function is to generate std::pair objects quickly by determining the types of their constituent parts from the arguments supplied to it. This function is especially useful when we need to quickly produce pairs of values without mentioning their kinds explicitly.

Syntax of std::make_pair():

The syntax of std::make_pair() is given below:

  • T1 and T2 represent the kinds of items in the pair.
  • The values that will be kept in the pair are value1 and value2.
  • A std::pair object with the requested values is returned by the std::make_pair()

Example 1:

Using std::make_pair() for Integer Pairs:

Let's begin with an easy example where we want to use std::create to construct a pair of integers make_pair():


Pair created: (10, 20)

As we can see, the pair of numbers we supplied were successfully constructed using std::make_pair().


In this example, two numbers named x and y are declare, and use make_pair(x, y) to make a pair of integers. The resulting pair is stored in the myPair variable, and its values are printed using myPair.first and myPair.second.

Example 2:

Making Pairs with std::make_pair() for Various Data Types

There is no restriction on pairs of the same data type for std::make_pair(). It can also be used to make pairs of other data kinds. Here is an example to demonstrate this:


Pair created: (42, Hello, C++!)

In this example, using std::make_pair(), we construct a pair that consists of an integer (number) and a string (text). After that, the program prints the values of the pair.

Use Case of std::make_pair():

1. Function Return Values

When a function has to return a pair of values, std::make_pair() is frequently utilized. The function can use std::make_pair() to automate the pair construction procedure rather than having to do it manually. Here's an example:


Quotient: 3, Remainder: 1

In this case, the dividend and remainder of the dividend by the divisor are returned by the divide function as a pair. A pair of zeros are returned as an error message if divisor is zero.

Performance Considerations:

Despite the ease of use and widespread use of std::make_pair(), it is crucial to be mindful of potential performance effects in crucial code parts. Directly building a pair might be more effective in some circumstances, particularly when dealing with move semantics or rvalue references. The code can be profiled and benchmarked to find such situations.

In conclusion, the C++ function std::make_pair() is useful for streamlining pair formation and resulting in cleaner, more legible code. It has several uses, including returning values from functions and interacting with common algorithms without any issues. In the code's performance-critical areas, it's crucial to strike a balance between practicality and performance considerations. We can use std::make_pair() efficiently in the C++ programming projects by being aware of its advantages and disadvantages.


The C++ function std::make_pair() is a potent pair-building utility that provides a clear and practical method for constructing pairs of different data kinds. The code will be easier to read and maintain because there will be no need to explicitly identify the kinds. The flexible function std::make_pair() makes the procedure easier and increases code readability whether we need pairs of integers, strings, or a mixture of data types.

We can write more effective and expressive code and have a more pleasant working experience by including std::make_pair() into our toolkit of C++ programming techniques.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA