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 initialized. You can use the following syntax to initialize pair.
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 can also be used to initialize a pair.
g2 = make_pair(1, 'a');
Pair declaration may also use the following syntax:
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:
Without explicitly defining the types, a value pair can be created using this template function.
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 contents of two pair objects are switched with the use of this function. Pairs must belong to the same category.
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:
This tie method works the same as tuples work. To unpack the tuple (or pair in this case) values into independent variables, it produces a tuple of lvalue pointers to its parameters. Here are two variations of the tie, one with and one without "ignore" , just like in tuples. The keyword "ignore" prevents a certain tuple element from being unpacked.
Pairs only have two arguments, but tuples can have numerous arguments. Therefore, unpacking needs to be dealt with explicitly in the case of 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