INTRODUCTION
PAIR IN STL
In this tutorial, we are going to learn about the pair() method from the standard template library of the C++ programming language. The pair() method is used to combine two values that may be of different data types. This is one of the unique ways to store two elements of different datatypes in a single unit. The pair() method is used if we want to store the data in tuples. We can implement the pair method by including the < utility > header.
Let’s look at the syntax of the pair() method.
pair (data_type1, data_type2) pair_name;
Where,
datatype_1: datatype of the first element
datatype_2: datatype of the second element
- The first element in the pair is referenced by the first and the second element in the pair is referenced by the second.
- The order of the pair is fixed, that is the first element in the tuple is referenced by the first and the second element in the tuple is referenced by the second.
- To access the values of the pair, we use the dot operator followed by the keyword first or second to access the specific value.
ASSIGNING VALUES TO A PAIR
The values of the pair can be assigned by the following methods.
Normal initialization:
pair (data_type1, data_type2) Pair_name (value1, value2);
Copying one pair to another:
pair<data_type1, data_type2> g3(value1, value2);
pair g4(g3);
Initialization using make_pair() method:
g1 = make_pair(value1, value2);
Initialization using curly braces:
g2 = {value1, value2};
Let’s look at some of the basic methods that are implemented on the pair
make_pair(): this member function is used to directly assign values to the pair regardless of assigning the datatypes of the two values. This method explicitly identifies the datatypes of the two values.
swap(): this member function is used to swap the values or the contents of one pair to another pair. The datatypes of the two values belonging to the two pairs must be the same.
tie(): tie is a member method that is used to pack the values of the pair. In this method, there is a special keyword that is used to unpack the values if needed, ignore is a special keyword used to unpack the value of the pair to the tuple.
Let’s look at a sample program implementing the basic methods on the pair.
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, int> pair4;
pair4 = make_pair(9,8); //make_pair() method
cout<<pair4.first<<" "<<pair4.second<<endl;
pair<int, int> pair5;
pair5.swap(pair4); //swap() method
cout<<"contents after swapping values of"<<endl;
cout<<"pair4 to pair5 :\n";
cout<<pair5.first<<" "<<pair5.second<<endl;
pair<int, int> pair1 = { 1, 2 };
int a, b;
tie(a, b) = pair1; // tie to pack values to tuples
cout << a << " " << b << "\n";
pair<int, int> pair2 = { 3, 4 };
tie(a, ignore) = pair2; // ignore keyword to print old value
cout<<a<<" "<< b<<endl; // of b
}
The output of the program is.
This is the implementation of the basic methods on the pair.
CONCLUSION
That’s it from this tutorial. We have learned about the pair method, its syntax, different ways to initialize the pair, basic methods implemented on the pair and a sample program implementing the pair method. I hope you guys found it interesting. Happy coding!