pair, tuple, and Structured Binding in C++
Learn pair, tuple, and structured binding in C++ to manage multiple values and write cleaner, more modern code.
The C++ Standard Library provides pair and tuple for storing multiple values within a single structure. With C++17, structured binding was introduced, offering a cleaner and more readable way to unpack these structures into separate variables. This article explains pair, tuple, and structured binding in detail with practical examples.
1) What is pair?
std::pair is a simple data structure that holds two values, which may be of different types. Pairs are widely used in functions that return two values.
- Contains two elements:
firstandsecond - Each element can have a different type
- Useful for returning multiple values from functions
#include <utility>
#include <iostream>
using namespace std;
int main() {
pair<string, int> person("Ali", 30);
cout << person.first << ", " << person.second << endl;
}
Output: Ali, 30
2) Using make_pair
auto p = make_pair("Ayse", 25);
cout << p.first << ", " << p.second;
3) Function Returning a pair
pair<int,int> getMinMax(int a, int b) {
return { min(a, b), max(a, b) };
}
int main() {
auto result = getMinMax(15, 40);
cout << "Min: " << result.first
<< ", Max: " << result.second;
}
4) What is tuple?
std::tuple is a generalized version of pair and can store more than two values, even if they are of different types. It is ideal for grouping heterogeneous values.
#include <tuple>
#include <iostream>
using namespace std;
int main() {
tuple<string, int, double> student("Mehmet", 22, 3.40);
cout << get<0>(student) << endl;
cout << get<1>(student) << endl;
cout << get<2>(student) << endl;
}
5) Using make_tuple
auto data = make_tuple("Book", 350, 49.90);
cout << get<0>(data) << endl;
cout << get<1>(data) << endl;
cout << get<2>(data) << endl;
6) tuple Size and Type Information
tuple<string, int, double> t;
cout << tuple_size<decltype(t)>::value << endl;
// tuple size = 3
// Extracting type information
using T0 = tuple_element<0, decltype(t)>::type;
7) Structured Binding (C++17)
Structured binding provides a clearer and more readable way to unpack tuple or pair values into individual variables.
auto student = make_tuple("Zeynep", 21, 3.75);
auto [name, age, gpa] = student;
cout << name << " - " << age << " - " << gpa;
8) Structured Binding with pair
pair<string, int> book("C++", 450);
auto [title, pages] = book;
cout << title << " - " << pages;
9) Returning a tuple from a Function
tuple<string,double,int> getProduct() {
return { "Laptop", 14500.99, 24 };
}
int main() {
auto [name, price, stock] = getProduct();
cout << name << ", "
<< price << ", "
<< stock;
}
10) Practical Example — User Info
#include <tuple>
#include <iostream>
using namespace std;
tuple<string,int,string> getUser() {
return { "Ahmet", 28, "Engineer" };
}
int main() {
auto [name, age, job] = getUser();
cout << name << " (" << age << ") - " << job;
}
11) When Should You Use Each?
- pair: When you only need to store two values.
- tuple: When you need to store more than two values of different types.
- structured binding: When you want clean and readable unpacking.
pair → simple two-value structure
tuple → multiple, heterogeneous values
structured binding → clean, readable extraction
12) TL;DR
- pair: Holds two values (first, second).
- tuple: Stores multiple values of different types.
- structured binding: Unpacks pair/tuple into meaningful variable names.
- All examples work on Visual Studio 2022 and GCC.
Related Articles
Iterators and Their Use in the C++ STL
Learn iterators in C++ and how they work with STL containers and algorithms to traverse and manipulate data efficiently.
Key-Value Structures with std::map and std::unordered_map in C++
Learn how to use std::map and std::unordered_map in C++ for efficient key-value storage, fast lookups, and practical applications.
Overview of the C++ Standard Library
Learn the C++ Standard Library, including STL containers, algorithms, iterators, and essential components for modern C++ development.