Pair in C++ Standard Template Library

C++ Standard Library provides a simple yet powerful tool called pair to store two values of different data types as a single unit. The pair is defined in the <utility> header file and can be used to store any two values, including values of built-in data types and user-defined data types.

Creating a Pair

A pair can be created by passing two values as arguments to its constructor. The values can be of any data type, including built-in data types and user-defined data types.

#include <iostream>
#include <utility>

int main() {
    std::pair<int, char> p1(1, 'A');
    std::cout << "p1: " << p1.first << ", " << p1.second << std::endl;
    return 0;
}

The output of the above program will be:

p1: 1, A

Accessing the Values in a Pair

The values stored in a pair can be accessed using the first and second members of the pair. These members are public and can be accessed directly as follows:

#include <iostream>
#include <utility>

int main() {
    std::pair<int, char> p1(1, 'A');
    std::cout << "p1: " << p1.first << ", " << p1.second << std::endl;
    p1.first = 2;
    p1.second = 'B';
    std::cout << "p1: " << p1.first << ", " << p1.second << std::endl;
    return 0;
}

The output of the above program will be:

p1: 1, A
p1: 2, B

Making a Pair using make_pair()

The make_pair() function is a convenient way to create a pair. It can be used to create a pair by passing two values as arguments. The values can be of any data type, including built-in data types and user-defined data types.

#include <iostream>
#include <utility>

int main() {
    auto p1 = std::make_pair(1, 'A');
    std::cout << "p1: " << p1.first << ", " << p1.second << std::endl;
return 0;
}

Comparing Pairs

Pairs can be compared using relational operators such as ==, !=, <, <=, >, and >=. The comparison is done based on the values stored in the pair. If the first values are equal, then the second values are compared. If the first values are different, then the comparison is based on the first values.

#include <iostream>
#include <utility>
int main() {
std::pair<int, char> p1(1, 'A');
std::pair<int, char> p2(2, 'B');
if (p1 == p2) {
std::cout << "p1 and p2 are equal" << std::endl;
} else {
std::cout << "p1 and p2 are not equal" << std::endl;
}
return 0;
}

The output of the above program will be:

p1 and p2 are not equal

Using Pair in STL Algorithms

Pairs can be used as elements in STL containers such as vector, list, and map. The STL algorithms such as sort, binary_search, and lower_bound can be used with pairs. The sorting and comparison of pairs is done based on the first values. If the first values are equal, then the second values are compared.

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
int main() {
std::vector<std::pair<int, char>> v;
v.push_back(std::make_pair(1, 'A'));
v.push_back(std::make_pair(2, 'B'));
v.push_back(std::make_pair(3, 'C'));
std::sort(v.begin(), v.end());
for (auto& p : v) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << std::endl;
return 0;
}

The output of the above program will be:

(1, A) (2, B) (3, C) 

Conclusion

In conclusion, the C++ pair is a useful construct for storing and managing pairs of values, especially when used with STL containers and algorithms. Pairs can be created using constructors, make_pair function, or by initializing with values. Pairs can be accessed using first and second members, and can be compared using relational operators. It’s important to note that the comparison is done based on the first values, and if they are equal, then the second values are compared. Pairs are widely used in C++ and provide a simple and efficient way to store and manage values.

Leave a Reply

Your email address will not be published. Required fields are marked *