// ── CREATE ───────────────────────────────────────────────────────
pair<int, string> p = {3, "a"}; // preferred
auto p = make_pair(1, 'A'); // alternative
// ── ACCESS ───────────────────────────────────────────────────────
p.first; p.second;
// ── RETURN FROM FUNCTION ─────────────────────────────────────────
pair<int,int> get() {
return {10, 20}; // short return
}
// auto return type
auto f(){
return make_pair(a,b);
}
// structured binding (best)
auto [x, y] = get(); // x=10, y=20
// ── VECTOR OF PAIRS ──────────────────────────────────────────────
vector<pair<int,int>> vp = {{3,4}, {1,5}};
vp.push_back({5,1}); // prefer {} over make_pair
// ── ITERATE ──────────────────────────────────────────────────────
for (auto &i : vp)
cout << i.first << " " << i.second;
// ── ITERATOR (verbose) ───────────────────────────────────────────
vector<pair<int,int>>::iterator it;
for (it = vp.begin(); it != vp.end(); it++)
cout << it->first << " " << it->second;
// ── COMPARE (lexicographic) ──────────────────────────────────────
{2,3} < {2,4}; // true
{1,9} < {2,0}; // true
// ── SORT (default) ───────────────────────────────────────────────
sort(vp.begin(), vp.end());
// ── SORT (custom) ────────────────────────────────────────────────
sort(vp.begin(), vp.end(), [](auto &a, auto &b) {
return a.second < b.second;
});
// ── PASS TO FUNCTION ─────────────────────────────────────────────
void print(vector<pair<int,int>> &v) {
for (auto &i : v)
cout << i.first << " " << i.second << "\n";
}