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;
|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++.
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.
C++ Pair STL Functions
|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, ...);|
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);|
Let us understand the make_pair member function in C++ with the help of the code given below:
25 A Ninjas_Coder 2.55 Coding_Ninjas 5.55
Using this function, one pair object's contents are switched for those of another pair object. Pairs must belong to the same category.
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.
Let us understand the swap member function in pair in C++ with the help of the code given below:
Contents of p1 = A 25 Contents of p2 = B 50 Contents of p1 = B 50 Contents of p2 = A 25
You can try and compile with the help of online c++ compiler.
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:
std::tie(var1, var2, ..., varn) = someTupleOrPair;
In this syntax:
- var1, var2, ..., varn are the variables you want to assign the values to.
- someTupleOrPair is the tuple or pair you want to unpack
Here is an example of how std::tie can be used:
Types of Operators Used in Pair in C++
Let us learn different types of operators that we use in pair in C++ with the help of a table given below:
|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.|
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 pair in data structure?
In data structures, a pair is a container that holds two values, often of different types, allowing the grouping of two related elements.
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.
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.
Do upvote our blog to help other ninjas grow.