Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
A simple container that holds two values of possibly different types. It's often used to return two values from a function or to store key-value associations.
#include <utility>
pair<string, int> p = {"Alice", 20};
cout << p.first; // "Alice"
cout << p.second; // 20
auto p2 = make_pair(10, 3.14);You can nest pairs to store 3 or more values: pair<int, pair<int, int>>.
Pairs support relational operators (<, >, etc.). They compare the first element, then the second.
Excellent for storing coordinates (x, y), graph edges (weight, node), or entries in a map.