C++

How to Use Make_Pair in C++

In many programming languages, you may have added one or more values into some data structure like list, dictionary, and array. But have you ever tried to add values in pairs like dictionaries? The make_pair() function of C++ is used to add two values in a pair within the pair object. So, we will be having an example of the make_pair() function in C++. Let’s get started with Ubuntu 20.04 and create a c++ file in the terminal. Open the file in the Nano editor.

$ touch makepair.cc

$ nano makepair.cc

Example 01:

Let’s get started with the first example of how to make a pair without using the make_pair() function in our code. The code has been started with the iostream and utility header library inclusion. After that, the namespace of C++, i.e., “std,” has been added. The main() function starts with declaring a new pair vector “P” having both integer type values. The pair object “P” has been used to add the values in the pair using the “first” and “second” keywords. Both the values added in the pair are integer types. The cout statement calls the pair values displayed on the shell with the pair object “P.”

#include <iostream>

#include <utility>

using namespace std;

int main() {

    pair<int, int> P;

    P.first = 14;

    P.second = 4;

    cout<<"Pair: "<<P.first<<","<<P.second<<endl;

    return 0;

}

The makepair.cc file has been compiled using Ubuntu’s G++ compiler and executed with the “./a.out” command. The pair have been displayed on the shell as shown.

$ g++ makepair.cc

$ ./a.out

Example 02:

Here comes the example of using the make_pair function to create a pair of two values. So, we have updated the same file. The header libraries, namespace, and main() function declaration are the same as the above example. We have declared a pair “P” having both its character type values. Using the make_pair() function, we have added two character type values into the pair “P” by assignment. This is the simplest way to use the make_pair method in C++. The values will be stored to the pair’s exact locations. The cout statement is here to get the pair values from the pair “P” by the keywords “first” and “second” and display it on the shell as a pair separated by a comma. The program has been completed here.

#include <iostream>

#include <utility>

using namespace std;

int main() {

    pair<char, char> P;

    P = make_pair('R','M');

    cout<<"Pair: "<<P.first<<","<<P.second<<endl;

    return 0;

}

We have compiled and executed the code file with g++ and ./a.out command on the terminal. It outputs the pair values separated by command as demonstrated in the image.

$ g++ makepair.cc

$ ./a.out

Example 03:

Here is another way to use the make_pair function in C++ to get values. Therefore, the code started with the same libraries, standard namespace, and the main() function. We have simply declared and initialized the C++ pair of string variables in a single line using the “pair” iterator. This line also contains the direct assignment of the first and second values of the pair with the help of a make_pair() function here. Two string values have been inserted into the pair “P.” The cout statement is here again to display these pair values on the shell separated by a command and called by pair object “P” with a first and second keyword.

#include <iostream>

#include <utility>

using namespace std;

    int main() {

    pair<string, string> P = make_pair("Hello", "Linux");

    cout<<"Pair: "<<P.first<<","<<P.second<<endl;

return 0;

}

While the compilation got successful, we have executed the code. The result shows the output display of string type pair values on the shell separated by a comma.

$ g++ makepair.cc

$ ./a.out

Example 04:

Let’s have our last example for this article. We have been using the same header files and namespace here again. The main() function is little same and little different from the above examples. It has been initialized with two string type pairs, P1 and P2, directly getting the string values using the “make_pair()” function. P1 and P2 have different string values in the first and second arguments. The first cout statement states that we will display the values of both pairs, P1, and P2 before swapping them. The next two cout statements display the values of pairs P1 and p2 separately via the first and second locations. The 4th cout statement is here to tell us that we are going to swap the values of both pairs to each other, i.e., p1 with p2. The use of the “swap” method to swap P1 with P2. The next cout statement displays the swapped values of the pairs.

#include <iostream>

#include <utility>

using namespace std;

int main() {

    pair<string, string> P1 = make_pair("Hello", "Linux");

    pair<string, string> P2 = make_pair("Good", "Bye");

    cout<<"Before Swap"<<endl;

    cout<<"Pair1: "<<P1.first<<","<<P1.second<<endl;

    cout<<"Pair2: "<<P2.first<<","<<P2.second<<endl;

    cout<<"After Swap"<<endl;

    P1.swap(P2);

    cout<<"Pair1: "<<P1.first<<","<<P1.second<<endl;

    cout<<"Pair2: "<<P2.first<<","<<P2.second<<endl;

    return 0;

}

After the code execution, the first three lines show the pairs’ values before the swap. While the last three lines are showing the values of pairs that have been swapped.

$ g++ makepair.cc

$ ./a.out

Conclusion:

This is all about making use of the make_pair() function in C++ to add two values in a pair variable at a time. You have to make your mind clear that you cannot swap two different types of pairs via the swap method. We have implemented four different examples for a better understanding of this concept.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.