set, pair & tuple in C++

From the C++ cheat sheet · STL Containers · verified Jul 2026

set, pair & tuple

Unique collections and fixed groupings.

cpp
// Quick Reference
#include <set>
std::set<int> s = {3, 1, 2};   // sorted, unique
std::pair<int, std::string> p{1, "a"};
auto t = std::make_tuple(1, 2.0, "x");
💡 std::set stores sorted, unique keys; inserting a duplicate is a no-op.
⚡ Destructure pairs/tuples with structured bindings: auto [a, b] = p.
📌 Access tuple elements by compile-time index: std::get<0>(t).
🟢 Prefer a small struct with named fields over a tuple when the meaning matters.
setpairtuple

More C++ tasks

Back to the full C++ cheat sheet