The C++ Standard Template Library (STL) provides the pair container to store two related values in a single object. It’s a simple yet powerful utility for representing relationships between two pieces of data, such as coordinates, key-value pairs, or any other linked information.
Key Features of pair
- Defined in
<utility>header. - Stores two values (of possibly different types) as
firstandsecond. - Lightweight alternative to custom structures or classes for quick use.
1. Declaring a pair
You can declare a pair with any two types:
#include <iostream>
#include <utility> // Required for pair
using namespace std;
int main() {
pair<int, string> myPair; // Declare a pair of int and string
myPair = {1, "Hello"}; // Initialize the pair
cout << "First: " << myPair.first << ", Second: " << myPair.second << endl;
return 0;
}
Output:
First: 1, Second: Hello
2. Initializing a pair
You can initialize a pair in different ways:
pair<int, string> p1(10, "Example"); // Using the constructor
pair<int, string> p2 = {20, "Hello"}; // Using initializer list
auto p3 = make_pair(30, "World"); // Using make_pair
cout << "p1: (" << p1.first << ", " << p1.second << ")\n";
cout << "p2: (" << p2.first << ", " << p2.second << ")\n";
cout << "p3: (" << p3.first << ", " << p3.second << ")\n";
Output:
p1: (10, Example)
p2: (20, Hello)
p3: (30, World)
3. Modifying Elements
You can directly modify the first and second elements of a pair:
pair<int, char> p(5, 'A');
p.first = 10; // Modify first element
p.second = 'B'; // Modify second element
cout << "Updated pair: (" << p.first << ", " << p.second << ")" << endl;
Output:
Updated pair: (10, B)
4. Using pair in a Container
Pairs are commonly used in containers like vector or map. For example:
Storing Pairs in a vector:
#include <vector>
vector<pair<int, string>> students = {
{1, "Alice"}, {2, "Bob"}, {3, "Charlie"}
};
for (const auto& student : students) {
cout << "ID: " << student.first << ", Name: " << student.second << endl;
}
Output:
ID: 1, Name: Alice
ID: 2, Name: Bob
ID: 3, Name: Charlie
5. Sorting a Vector of Pairs
Pairs can be sorted by their first or second values. By default, pair is compared lexicographically (first is compared first, then second if first is equal).
#include <algorithm>
vector<pair<int, string>> items = {
{3, "Apple"}, {1, "Banana"}, {2, "Cherry"}
};
// Sort by the first value (default behavior)
sort(items.begin(), items.end());
cout << "Sorted pairs:\n";
for (const auto& item : items) {
cout << "(" << item.first << ", " << item.second << ")\n";
}
Output:
Sorted pairs:
(1, Banana)
(2, Cherry)
(3, Apple)
6. Common Use Cases
- Storing (x, y) coordinates:
pair<int, int> coordinate = {3, 4}; cout << "Coordinate: (" << coordinate.first << ", " << coordinate.second << ")" << endl; - Returning multiple values from a function:
pair<int, string> getStudent() { return {123, "John Doe"}; } auto student = getStudent(); cout << "ID: " << student.first << ", Name: " << student.second << endl; - Key-Value pairs in a
map:map<int, string> studentMap = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} }; for (const auto& entry : studentMap) { cout << "ID: " << entry.first << ", Name: " << entry.second << endl; }
Summary
pairis a simple and efficient way to represent two related pieces of data.- It can hold any two types and is especially useful for coordinates, key-value pairs, or temporary data structures.
- Use it in combination with STL containers for more complex operations.
With these basics, you are ready to start using pair effectively in your C++ programs!
