Introduction to C++ pair
In C++, pair is defined as a container in a header library <utility> which combines the two data elements having either the same data types or different data types. In general, the pair in C++ is defined as a tuple in Python programming language which also can give the output as a combined result of joining the two items specified by the pair container and it consists of the first element will be first and the second element will be second only it cannot be disturbed in the order or sequence of elements specified and are always accessed by the dot operator followed by the keyword “first” and “second” elements respectively.
Working of pair in C++ with few Examples
In this article, we will see pair container in C++ and how it is used. In C++, pair container behaves like a tuple in Python programming language but a tuple can have a list of items whereas pair can have only two items or elements which can be of different data types or the same datatype as in tuple. The declaration of pair in C++ is done using the keyword “pair” and is a container that is provided from <utility> library. So basically, pair is used for joining two elements or values into one which also allows storing items of different data types or two heterogeneous objects into one single unit.
The pair container can store only two elements first element in “first” and can be referenced by “first” only and the second element can be only in “second”. We can use operators such as =, !=, = =, >=, <= with pair and also we can swap the one content of one pair with other pair also using the swap() function and there is also a feature where we can create a value pair without declaring the datatypes explicitly using make_pair() function where we need not specify the datatype and write the values directly.
Syntax of how to declare a pair:
In C++ the pair is a container in <utility> header and is also a container class in STL (Standard Template Library) which uses “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) pairname;
- dt1: datatype for the first element.
- dt2: datatype for the second element.
- pairname: a name which is used to refer to the pair objects .first and .second elements.
So now we will see an example using <utility> header and declaring pair container.
using namespace std;
std::pair<int, int> pairname;
pairname.first = 5;
pairname.second = 7;
std::cout << "First number is: " << pairname.first << std::endl;
std::cout << "Second number is: " << pairname.second << std::endl;
In the above program, we can see we are including <utility> header for using pair container and <iostream> header for printing the message or for input or output and namespace is used for using std are declared at the starting and then as per the syntax we can use the pair container here we are declaring items with the same data type “int” we can even take different datatype also. So we are just printing the integer values of the pair variables in the output as we can see in the above screenshot.
Therefore, when we are using std namespace the syntax will be as follows:
std::pair<datatype var1, datatype var2> pairname;
In this, we can declare variables of any data type or same datatypes such as int, float, double, string, char, etc for var1 and var2.
So to define or access or initializing the values to each element in the pair container is done as follows in the below:
pairname.first to define the first element or to access it.
pairname.second to define the second element or to access it.
Note that we cannot change the order of variables and its datatype while declaring and defining the pair.
using namespace std;
std::pair<string, float> pairname;
pairname.first = "Educba";
pairname.second = 2.0;
std::cout << "The first item in the pair container is : " << pairname.first << std::endl;
std::cout << "The second item in the pair container is : " << pairname.second << std::endl;
In the above program, we can see we are not using <utility> header now as in the first example. So in this example, we can see we have declared a pair variable one with “string” datatype and second item with “float” data type. So we can see in the output screenshot we can also print the values of the elements in the pair variable of the different datatype. The most important is to note that when we are referring to the first item of “string” type we refer to it as “pairname.first” to initialize the value to the first item and similarly, the second item is referred to as “pairname.second” to initialize the value to the second item.
Now we will see how to swap the pair object using swap and operators such as =, !=, >=, <=, etc can be used with pair container to yield the results and let us also demonstrate the make_pair() function which is used for not specifying the datatypes and still we can execute the program in below example.
using namespace std;
pair<int, int>pair1 = make_pair(90, 100);
pair<int, int>pair2 = make_pair(4, 30);
cout<< "Use of opertaors with pair and it results in true (1) or false (0)";
cout << (pair1 <= pair2) << endl;
cout << (pair1 >= pair2) << endl;
cout << (pair1 > pair2) << endl;
cout << (pair1 < pair2) << endl;
cout << (pair1 == pair2) << endl;
cout << (pair1 != pair2) << endl;
cout << "Use of swap function with pair";
cout << "Before swapping:\n" ;
cout << "Contents of pair1 = " << pair1.first << " " << pair1.second << "\n";
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second << "\n";
cout << "\nAfter swapping:\n";
cout << "Contents of pair1 = " << pair1.first << " " << pair1.second << "\n " ;
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second << "\n" ;
In the above program, we can see we demonstrating the make_pair() function where we can see in pair1 we int type, and the values are initialized without specifying any datatypes. Then we saw which operators can work with pair concept one thing to note is that both pair variables must have the same datatype for both variables within each pair for comparison operators else it will give an error. Then we saw how to use swap function with pair where it will not only swap the variables within the pair but also swaps the pairs defined in this program.
In this article, we conclude that the pair container is obtained from <utility> header and can use namespace std also in C++ where this pair container behaves similarly to that of the tuple in Python as it can also hold both elements in pair variable of the same or different data types. The pair container in C++ is mainly used for combining the two elements into one single unit irrespective of the datatypes of the elements in it. In this article, we also saw we can use swap() and operators with pair in this article we have given an example for comparison operators.
This is a guide to C++ pair. Here we discuss the introduction and working of pair in C++ along with few examples respectively. You may also have a look at the following articles to learn more –