
A Beginner’s Guide to C++ pair: Make, Access, Swap, Compare
The pair class in the C++ Standard Template Library (STL) is a simple yet powerful utility that lets you group two values into a single object. This is extremely useful when dealing with related data like coordinates, key-value pairs, etc.
In this post, we’ll go over:
- Basic usage of pair
- Practical code example
- All built-in functionalities available for pair
- Array of pairs
- Swapping and comparing pairs
What is a pair?
A pair<T1, T2> is a templated class that stores two heterogeneous or homogeneous values. It's commonly used when:
- You want to return two values from a function
- Store key-value mappings
- Store data points with two attributes (like a coordinate)
📌 The Code Example
Let’s start with a simple code that introduces pair, arrays, input/output, and swapping:
cpp Edit#include <bits/stdc++.h> using namespace std; int main() { cout << "hello" << endl; pair<int, string> p; p = make_pair(2, "abc"); cout << p.first << " " << p.second << endl; int a[] = {1, 2, 3}; int b[] = {4, 5, 6}; pair<int, int> parr[3]; for (int i = 0; i < 3; i++) { parr[i] = {a[i], b[i]}; } swap(parr[0], parr[1]); swap(parr[0], parr[2]); for (int i = 0; i < 3; i++) { cout << parr[i].first << " " << parr[i].second << endl; } return 0; }
🔍
✅ Step 1: Using pair<int, string>
pair<int, string> p; p = make_pair(2, "abc"); cout << p.first << " " << p.second << endl;
Here we create a pair of an int and a string. The make_pair function is used to assign values. The output will be:
2 abc
✅ Step 2: Pairing Two Arrays
int a[] = {1, 2, 3}; int b[] = {4, 5, 6}; pair<int, int> parr[3];
We define two integer arrays a and b, and then we pair elements from a and b together.
for (int i = 0; i < 3; i++) { parr[i] = {a[i], b[i]}; }
After this loop:
- parr[0] = {1, 4}
- parr[1] = {2, 5}
- parr[2] = {3, 6}
✅ Step 3: Swapping Pairs
swap(parr[0], parr[1]); // parr[0] <-> parr[1] swap(parr[0], parr[2]); // new parr[0] <-> parr[2]
Let’s visualize it:
- After first swap:
parr[0] = {2, 5}, parr[1] = {1, 4}, parr[2] = {3, 6} - After second swap:
parr[0] = {3, 6}, parr[2] = {2, 5}
Final state:
parr[0] = {3, 6} parr[1] = {1, 4} parr[2] = {2, 5}
✅ Step 4: Output Final Pairs
3 6 1 4 2 5
Edit
for (int i = 0; i < 3; i++) { cout << parr[i].first << " " << parr[i].second << endl; }
Expected output:
3 6 1 4 2 5
✅ Declaring and Initializing Pairs
🔹 Using make_pair()
pair<int, string> p = make_pair(2, "abc");
🔹 Using Direct Initialization (C++11 onwards)
pair<int, string> p = {2, "abc"};
📋 Built-in Functionalities of pair
Let’s go through all useful operations and functions associated with pair:
1. ✅ .first and .second
- Access the first and second values.
cout << p.first << " " << p.second;
2. ✅ make_pair()
- Creates a pair without explicitly writing types.
auto p = make_pair(10, "hello");
3. ✅ Comparison Operators
- pair supports <, <=, >, >=, ==, !=
- Comparison is lexicographic: first compares first, then second.
pair<int, int> a = {1, 5}, b = {1, 7}; if (a < b) cout << "a is less";
4. ✅ swap(pair1, pair2)
- Swaps the contents of two pairs.
swap(p1, p2);
5. ✅ Copy Assignment and Copy Constructor
pair<int, string> p1 = {1, "one"}; pair<int, string> p2 = p1; // Copy
6. ✅ Nested Pairs
Yes, pairs inside pairs!
pair<int, pair<int, int>> nested = {1, {2, 3}}; cout << nested.first; // 1 cout << nested.second.first; // 2
🧪 Arrays and Vectors of Pairs
✅ Array of Pairs
pair<int, int> arr[3]; arr[0] = {1, 4}; arr[1] = {2, 5}; arr[2] = {3, 6};
✅ Vector of Pairs
vector<pair<int, string>> v; v.push_back({1, "one"}); v.emplace_back(2, "two");
🎯 Real-World Use Cases
- Graph Representation: Edges as pair<int, int>
- Sorting with Criteria: Sort a list of students with {marks, roll}
- Dictionaries and Maps: STL map<int, string> internally uses pair<int, string>
0 Comments