// ── CREATE ───────────────────────────────────────────────────────
pair<int, string> p = {3, "a"}; // preferred
auto p = make_pair(1, 'A'); // alternative
// ── ACCESS ───────────────────────────────────────────────────────
p.first; p.second;
// ── 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) // & avoids copy
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; // -> cleaner than (*it).first
// ── COMPARE (lexicographic) ──────────────────────────────────────
{2,3} < {2,4} // first equal → compare second → 3 < 4 → true
{1,9} < {2,0} // first differs → 1 < 2 → true
// ── SORT (default) ───────────────────────────────────────────────
sort(vp.begin(), vp.end()); // by first → ties by second
// ── SORT (custom) ────────────────────────────────────────────────
sort(vp.begin(), vp.end(), [](auto &a, auto &b) {
return a.second < b.second; // sort by second
});
// ── PASS TO FUNCTION ─────────────────────────────────────────────
void print(vector<pair<int,int>> &v) { // & avoids copy
for (auto &i : v)
cout << i.first << " " << i.second << "\n";
}