std::pair
std::pair
is a simple but powerful utility in C++ that is used to store two heterogeneous objects (values) together. The objects can be of any type, and they are stored as members named first and second. std::pair
is particularly useful when you need to return two values from a function, store key-value pairs in containers like std::map
, or combine two related pieces of data.
std::pair
std::pair
can store two values of different types.std::pair
supports comparison operators, allowing pairs to be compared lexicographically.std::pair
is easy to use and integrates seamlessly with many parts of the C++ Standard Library, such as std::map
, std::set
, and std::tuple
.Let's explore some basic examples to understand how std::pair works.
Example 1: Creating and Using std::pair
#include <iostream>
#include <utility> // For std::pair
int main() {
// Create a pair to store an integer and a string
std::pair<int, std::string> myPair(1, "One");
// Access the elements of the pair
std::cout << "First: " << myPair.first << std::endl;
std::cout << "Second: " << myPair.second << std::endl;
return 0;
}
myPair
is created with an integer 1 as the first element and a string "One" as the second element.std::make_pair
C++ provides the utility function std::make_pair
to create pairs more conveniently without specifying the types explicitly.
#include <iostream>
#include <utility>
int main() {
// Create a pair using std::make_pair
auto myPair = std::make_pair(2, "Two");
// Access the elements of the pair
std::cout << "First: " << myPair.first << std::endl;
std::cout << "Second: " << myPair.second << std::endl;
return 0;
}
std::make_pair
: Automatically deduces the types of the elements in the pair, making it easier to create pairs without specifying the types explicitly.One of the common uses of std::pair is to return multiple values from a function.
#include <iostream>
#include <utility>
std::pair<int, int> getMinMax(int a, int b) {
if (a < b)
return std::make_pair(a, b);
else
return std::make_pair(b, a);
}
int main() {
auto result = getMinMax(10, 20);
std::cout << "Min: " << result.first << std::endl;
std::cout << "Max: " << result.second << std::endl;
return 0;
}
std::pair is often used in associative containers like std::map to store key-value pairs.
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
// Insert key-value pairs using std::pair
myMap.insert(std::make_pair(1, "One"));
myMap.insert(std::make_pair(2, "Two"));
// Iterate over the map
for (const auto& pair : myMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}
#include <iostream>
#include <utility>
int main() {
std::pair<int, std::string> pair1(1, "Apple");
std::pair<int, std::string> pair2(2, "Banana");
if (pair1 < pair2) {
std::cout << "pair1 is less than pair2" << std::endl;
}
return 0;
}
pair1 < pair2
first compares the first members (1 < 2
), so pair1
is considered less than pair2.
std::pair
supports all relational operators (==, !=, <, >, <=, >=)
.std::pair
std::pair
is a straightforward solution.std::pair
is the fundamental building block of associative containers like std::map
and std::multimap,
where it is used to store key-value pairs.std::pair
provides a lightweight and efficient solution.std::pair:
A simple container that holds two values of possibly different types, accessed via first and second.std::pair
is flexible and can be used to group any two types of data together.std::make_pair
provides an easy way to create std::pair
objects without explicitly specifying the types.std::pair
objects can be compared using standard comparison operators, with lexicographical ordering.std::pair
is a fundamental and versatile part of the C++ Standard Library, offering an easy way to store and work with pairs of related values. Its integration with containers like std::map makes it an indispensable tool in many C++ applications.