Pair is a term used to combine two values, which may be of various data kinds. Pair offers a technique to keep two disparate objects together in storage. Essentially, we use it to store tuples . A pair container is a straightforward container that is specified in the header and contains two data elements or objects.
- The sequence is fixed, and the first element is referred to as "first" and the second as "second" (first, second) .
- It is possible to assign, copy, and compare a pair. The array of objects allocated in a map or hash map is by default of type "pair" , meaning that all of the "first" components are distinct keys connected to objects with their "second"
- We utilize the variable name, the dot operator, the word first or second , and the element we want to access.
Syntax for pair in C++:
pair <data_type1, data_type2> Pair_name
Example:
A program for pair in C++;
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, char> PAIR1;
PAIR1.first = 200;
PAIR1.second = 'J';
cout << PAIR1.first << " ";
cout << PAIR1.second << endl;
return 0;
}
Output
Pair Initialization:
A pair can also be created by utilizing the syntax provided below for initialization.
Syntax:
pair (data_type1, data_type2) Pair_name (value1, value2) ;
Various approaches to pair initialization:
pair g1;
pair g2(1, 'a');
pair g3(1, 10);
pair g4(g3);
The make_pair function is also applicable for initializing a pair.
g2 = make_pair(1, 'a');
Pair declaration can alternatively utilize the syntax below:
g2 = {1, 'a'};
Example:
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<string, double> PAIR2("JavaCppTutorial", 2.23);
cout << PAIR2.first << " ";
cout << PAIR2.second << endl;
return 0;
}
Output
JavaCppTutorial 2.23
Note: If the pair's first value is not initialized, it is initialized automatically.
Example:
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, double> PR1;
pair<string, char> PR2;
cout << PR1.first;
cout << PR1.second;
cout << " ";
cout << PR2.first;
cout << PR2.second;
return 0;
}
Output
Member Functions
1) make_pair:
A key-value pair can be generated using this template function without explicitly specifying the data types.
Syntax:
Pair_name = make_pair (value1,value2);
Example:
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, char> PAIR1;
pair<string, double> PAIR2("JavaCppTutorial", 1.23);
pair<string, double> PAIR3;
PAIR1.first = 200;
PAIR1.second = 'J';
PAIR3 = make_pair("JavaCppTutorial is super", 4.56);
cout << PAIR1.first << " ";
cout << PAIR1.second << endl;
cout << PAIR2.first << " ";
cout << PAIR2.second << endl;
cout << PAIR3.first << " ";
cout << PAIR3.second << endl;
return 0;
}
Output
200 J
JavaCppTutorial 1.23
JavaCppTutorial is super 4.56
2) swap:
The function is utilized to interchange the values stored in two pair objects. It is important to note that these pairs should be categorized similarly.
Syntax:
pair1.swap(pair2) ;
Example:
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<char, int> pair1 = make_pair('P', 1);
pair<char, int> pair2 = make_pair('Q', 2);
cout << "content before swapping:\n ";
cout << "Contents of pair1 = " << pair1.first << " "
<< pair1.second;
cout << "Contents of pair2 = " << pair2.first << " "
<< pair2.second;
pair1.swap(pair2);
cout << "\n content after swapping:\n ";
cout << "Contents of pair1 = " << pair1.first << " "
<< pair1.second;
cout << "Contents of pair2 = " << pair2.first << " "
<< pair2.second;
return 0;
}
Output
content before swapping:
Contents of pair1 = P 1Contents of pair2 = Q 2
content after swapping:
Contents of pair1 = Q 2Contents of pair2 = P 1
3) tie:
The tie function operates similarly to how tuples function. It allows for the unpacking of tuple (or pair) values into separate variables by creating a tuple of lvalue pointers to the parameters. There are two versions of the tie function available—one with and one without the "ignore" keyword, mirroring the behavior seen in tuples. The "ignore" keyword serves to exclude a specific tuple element from being unpacked.
While pairs consist of only two elements, tuples can contain multiple elements. As a result, unpacking must be explicitly handled when working with pairs.
Syntax:
tie(int &, int &) = pair1;
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
pair<int, int> pair1 = { 10, 12 };
int p, q;
tie(p, q) = pair1;
cout << p << " " << q << "\n";
pair<int, int> pair2 = { 16, 18 };
tie(p, ignore) = pair2;
cout << p << " " << q << "\n";
pair<int, pair<int, char>> pair3 = { 16, { 18, 'p' } };
int x, y;
char z;
x = pair3.first;
tie(y, z) = pair3.second;
cout << x << " " << y << " " << z << "\n";
}
Output
10 12
16 12
16 18 p