![]() |
VOOZH | about |
Pair is a simple container that holds two values together. These two values can be of different types, and they are stored as a single unit.
The pair container has the following applications.
1 : Geeks
Syntax:
The pair container is defined in <utility> header file.
pair <T1, T2> p;
where,
In C++, pair can be declared and initialized in multiple ways as shown below:
1 Apple
Note: In pair, first and second values are stored as data members. So, we can access them by using their name with (.) operator.
2 Banana
3 Cherry
Number: 1 Text: Geeks
In pair, first and second values are stored as data members. So, we can access them by using their name with (.) operator.
For example:
p.first // returns first value
p.second // returns second value
We can update the values in a pair by directly changing .first and .second.
2 ForGeeks
Just like other data types, we can use relational operators with pairs. They initially compare the first value. If it does not give result, then second value is compared. The following table describes the behaviour of these operators for pairs:
| Operator | Description |
|---|---|
| == | Returns true if both pairs are equal, otherwise false. |
| != | Returns true if pairs are not equal, otherwise false. |
| > | Returns true if the LHS pair is greater than the RHS pair, otherwise false. |
| < | Returns true if the left operand is less than the right operand, otherwise false. |
| >= | Returns true if the left operand is greater than or equal to the right operand, otherwise false. |
| <= | Returns true if the left operand is less than or equal to the right operand, otherwise false. |
p1 == p2: 0 p1 != p3: 1 p1 > p3: 1 p1 < p2: 1 p1 >= p3: 1 p3 <= p1: 1