The pair in the C++ Standard Template Library (STL) is a simple and versatile utility to store two related values in a single object.
It is often used in containers, like map or set, where pairs of values are required. pair can store two elements of potentially different data types and can be accessed directly by first and second.
Key Features of pair
- Two Values: Stores two values, which can be of different types.
- Easy Access: Access the values using first and second.
- Comparison Operators: Supports comparison operators (==, <, >, etc.).
- Versatile: Useful in complex containers like map and multimap.
Basic Syntax
#include <utility> using namespace std; pair<Type1, Type2> p;
Let’s explore pair with various examples.
1. Initializing a pair
A pair can be initialized with values directly or by using the make_pair function.
#include <iostream> #include <utility> using namespace std; int main() { // Initialize using constructor pair<string, int> person1("Alice", 25); // Initialize using make_pair pair<string, int> person2 = make_pair("Bob", 30); // Display pairs cout << "Person 1: " << person1.first << ", Age: " << person1.second << endl; cout << "Person 2: " << person2.first << ", Age: " << person2.second << endl; return 0; }
Output:
Person 1: Alice, Age: 25 Person 2: Bob, Age: 30
2. Accessing Elements
You can access the two elements in a pair using the first and second members.
#include <iostream> #include <utility> using namespace std; int main() { pair<string, double> product("Laptop", 799.99); // Accessing elements cout << "Product: " << product.first << endl; cout << "Price: $" << product.second << endl; return 0; }
Output:
Product: Laptop Price: $799.99
3. Using make_pair to Create a pair
The make_pair function provides a convenient way to create a pair without specifying the types explicitly.
#include <iostream> #include <utility> using namespace std; int main() { // Using make_pair to create a pair auto book = make_pair("1984", 12.99); // Display pair cout << "Book: " << book.first << endl; cout << "Price: $" << book.second << endl; return 0; }
Output:
Book: 1984 Price: $12.99
4. Comparing Pairs
Pairs support comparison operators such as ==, <, >, etc., which compare first and second values lexicographically.
#include <iostream> #include <utility> using namespace std; int main() { pair<int, int> p1(1, 5); pair<int, int> p2(1, 10); // Compare pairs if (p1 < p2) { cout << "p1 is less than p2" << endl; } else { cout << "p1 is not less than p2" << endl; } return 0; }
Output:
p1 is less than p2
5. Swapping Pairs
The swap function exchanges the contents of two pair objects.
#include <iostream> #include <utility> using namespace std; int main() { pair<string, int> p1("Alice", 25); pair<string, int> p2("Bob", 30); // Swapping pairs p1.swap(p2); // Display swapped pairs cout << "After swap:" << endl; cout << "p1: " << p1.first << ", " << p1.second << endl; cout << "p2: " << p2.first << ", " << p2.second << endl; return 0; }
Output:
After swap: p1: Bob, 30 p2: Alice, 25
6. Using Pairs in Containers
pair is commonly used with STL containers such as vector, map, and set.
#include <iostream> #include <vector> #include <utility> using namespace std; int main() { vector<pair<string, int>> people; // Adding pairs to a vector people.push_back(make_pair("Alice", 25)); people.push_back(make_pair("Bob", 30)); people.push_back(make_pair("Charlie", 35)); // Display vector of pairs for (const auto &person : people) { cout << person.first << ": " << person.second << endl; } return 0; }
Output:
Alice: 25 Bob: 30 Charlie: 35
7. Using Pairs with map
map uses pair to store key-value pairs, where first represents the key and second represents the value.
#include <iostream> #include <map> using namespace std; int main() { map<string, int> age; // Adding pairs to the map age.insert(make_pair("Alice", 25)); age.insert(make_pair("Bob", 30)); age["Charlie"] = 35; // Display map elements for (const auto &entry : age) { cout << entry.first << ": " << entry.second << endl; } return 0; }
Output:
Alice: 25 Bob: 30 Charlie: 35
8. Pair Inside Another Pair
You can nest pairs within pairs, allowing more complex data structures.
#include <iostream> #include <utility> using namespace std; int main() { // Pair inside a pair pair<int, pair<string, double>> product = make_pair(1, make_pair("Laptop", 799.99)); // Access nested pair cout << "Product ID: " << product.first << endl; cout << "Product Name: " << product.second.first << endl; cout << "Price: $" << product.second.second << endl; return 0; }
Output:
Product ID: 1 Product Name: Laptop Price: $799.99
9. Initializing Pair Arrays
You can create arrays or vectors of pairs for storing multiple pairs.
#include <iostream> #include <utility> using namespace std; int main() { // Array of pairs pair<string, int> students[] = { make_pair("Alice", 25), make_pair("Bob", 30), make_pair("Charlie", 35) }; // Display array of pairs for (const auto &student : students) { cout << student.first << ": " << student.second << endl; } return 0; }
Output:
Alice: 25 Bob: 30 Charlie: 35
10. Pair as Return Type from Functions
A pair can be a convenient return type for functions that need to return two values.
#include <iostream> #include <utility> using namespace std; // Function returning a pair of name and age pair<string, int> getPerson() { return make_pair("Alice", 25); } int main() { pair<string, int> person = getPerson(); // Display pair returned by function cout << "Name: " << person.first << endl; cout << "Age: " << person.second << endl; return 0; }
Output:
Name: Alice Age: 25
Summary Table of pair Operations
Operation | Code Example | Description |
---|---|---|
Initialize | pair<string, int> p(“Alice”, 25); | Initializes a pair with values |
Access Elements | p.first, p.second | Accesses first and second values |
make_pair | make_pair(“Alice”, 25); | Creates a pair with automatic type inference |
Compare Pairs | p1 < p2, p1 == p2 | Compares pairs lexicographically |
Swap Pairs | p1.swap(p2); | Swaps contents of two pairs |
Use in Containers | vector<pair<string, int>> vp; | Stores pairs in STL containers |
Nested Pairs | pair<int, pair<string, double>> p; | Creates a pair with another pair inside |
Arrays of Pairs | pair<string, int> arr[]; | Creates an array of pairs |
Return Type | pair<string, int> func(); | Uses pair as a function return type |
Complete Example
This example demonstrates initializing pairs, using make_pair, nesting pairs, comparing pairs, swapping pairs, and storing pairs in a container.
#include <iostream> #include <utility> #include <vector> using namespace std; int main() { // Initialize pairs pair<string, int> p1("Alice", 25); pair<string, int> p2 = make_pair("Bob", 30); // Compare pairs if (p1 < p2) { cout << "p1 is less than p2" << endl; } // Swap pairs p1.swap(p2); cout << "After swap:" << endl; cout << "p1: " << p1.first << ", Age: " << p1.second << endl; cout << "p2: " << p2.first << ", Age: " << p2.second << endl; // Nested pair pair<int, pair<string, double>> product = make_pair(1, make_pair("Laptop", 799.99)); cout << "Product ID: " << product.first << endl; cout << "Product Name: " << product.second.first << endl; cout << "Price: $" << product.second.second << endl; // Store pairs in a vector vector<pair<string, int>> people = {make_pair("Charlie", 35), make_pair("Dave", 28)}; cout << "People in vector:" << endl; for (const auto &person : people) { cout << person.first << ": " << person.second << endl; } return 0; }
Sample Output:
p1 is less than p2 After swap: p1: Bob, Age: 30 p2: Alice, Age: 25 Product ID: 1 Product Name: Laptop Price: $799.99 People in vector: Charlie: 35 Dave: 28
Key Takeaways
- pair is a versatile utility that stores two related values, making it ideal for key-value pairs in containers.
- Access the values with first and second, and use make_pair for easy initialization.
- Comparisons and swapping make pair useful in containers, and it works well in complex structures like map and nested containers.