Pair in c++ is used to combine two elements of the same data types or different data types in C++. Pair is basically used if we want to store tuples and it also provides a way to store two heterogeneous objects as a single unit.
In this blog, we will read Pair in C++. So without any further ado, let's get started!
What is Pair in C++?
In C++, a pair is a container for two values (like a tuple). The datatype of these values might or might not be the same.
The name of the pair is followed by comma-separated data types enclosed in < > signs and the keyword "pair" in the declaration.
The first and second keywords can be used to access the elements of a pair.
Syntax to Declare a Pair in C++
In C++, the pair is a container in <utility> header and is also a container class in STL (Standard Template Library) which uses the “std” namespace so it will be as std::pair template class for demonstrating pair as a tuple.
In general, the syntax of pair can be defined as below:
pair(dt1, dt2) pair_name;
Parameters of Pair in C++
dt1
The first element's data type.
dt2
The first element's data type.
pair_name
It is the name given to the pairs of object.
You can also do a free certification of Basics of C++.
Example of Pair in C++
Let us understand the syntax with the help of the code given below:
In the programme mentioned above, we can see that we have included the <utility> header for using pair containers, the <iostream> header for printing messages or for input or output, and the namespace is used for using std. Additionally, we have declared the namespace at the beginning. Next, according to the syntax, we can use the pair containers to declare items with the same data type, "int" though we can also use different datatypes.
STL Functions of C++ Pair
Function
Description
Syntax
make_pair()
Creates a pair object with two elements.
std::make_pair(element1, element2);
swap()
Swaps the values of two variables.
std::swap(var1, var2);
tie()
Creates a tuple of references to variables.
std::tie(var1, var2, ...);
1. make_pair
By using the template feature, you can create a value pair in C++ without having to write the forms.
pair_name = make_pair (value_1,value_2);
Example
Let us understand the make_pair member function in C++ with the help of the code given below:
C++
C++
#include <iostream> #include <utility> using namespace std; int main()
Using this function, one pair object's contents are switched for those of another pair object. Pairs must belong to the same category.
Syntax
pair_1.swap(pair_2);
The swap function will swap pair_1.first with pair_2.first and pair_1.second with pair_2.second for two given pairs of the same type, pair_1 and pair_2.
Example
Let us understand the swap member function in pair in C++ with the help of the code given below:
C++
C++
#include <iostream> #include<utility> using namespace std;
The std::tie() function is a utility function in C++ that creates a tuple of lvalue references. It's commonly used in conjunction with std::pair and std::tuple for unpacking values.
The syntax for std::tie() is quite straightforward. It takes any number of arguments, each being a non-const lvalue reference, and returns a tuple of lvalue references to these arguments. Here's the general syntax:
Let us learn different types of operators that we use in pair in C++ with the help of a table given below:
Operators
Description
Equal(=)
It gives a pair of object a new assignment.
Comparison(==)
The comparison operator compares the “first value and the second value of those two pairs, i.e. if pair_1.first is equal to pair_2.first or not” and “if pair_1.second is equal to pair_2.second or not” for the two given pairs, pair1 and pair2.
Not equal(!=)
The != operator compares the first values of the two pairs, pair_1 and pair_2, to determine whether or not they are equal. If they are, the second values of both pairs are then checked.
Logical(>=,<=)
Pairs can also be used with the = and > operators for the two given pairs, pair_1 and pair_2. By only comparing the first value of the pair, it returns either 0 or 1.
What are Nested Pairs in C++
Nested pairs refer to a data structure where elements are organized in pairs, and these pairs can themselves contain other pairs, forming a nested structure. This nesting can continue to any depth, creating hierarchical relationships between elements. Nested pairs are commonly used in various contexts, including representing hierarchical data, defining tree structures, and implementing complex data structures like nested lists or dictionaries. In C++, nested pairs can be implemented using the std::pair and std::vector containers. Here's an example:
// Accessing elements cout << "First element of the outer pair: " << nestedPair.first.first << endl; cout << "Second element of the inner pair: " << nestedPair.second.second << endl;
return 0; }
You can also try this code with Online C++ Compiler
First element of the outer pair: 1
Second element of the inner pair: 4
Explanation: In this example, nestedPair is a nested pair containing two pairs of integers. Each pair represents a set of coordinates. The first member of nestedPair contains the first pair (1, 2), and the second member contains the second pair (3, 4). You can access elements of the nested pairs using dot notation or std::get.
Frequently Asked Questions
Why is pair in C++ used?
Pair is a term used to combine two values, which may be of various data types. Pair offers a way to keep two disparate objects together in storage. Essentially, we use it to store tuples.
What is the use of make pair in C++?
make_pair in C++ is used to create a pair object conveniently by providing two values, which simplifies the process of initializing pairs.
What is an example of a function pair?
An example is std::pair<int, string> myPair = std::make_pair(42, "Hello");, creating a pair of integer and string values.
Conclusion
In this article, we have discussed how to use a pair in C++ along with its various member functions. The pair in C++ proves to be a versatile and essential component for managing pairs of related values. Its simplicity, coupled with functions like make_pair, streamlines data handling. Whether in algorithms or data structures, the pair serves as a powerful tool, enhancing the efficiency and readability of C++ code.
To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.