Chapter 08 of 30
Vectors, Pairs & Utility Algorithms
Master std::vector, std::pair, std::tuple, iterators, and the STL utility algorithms every competitive programmer needs.
std::vector — The Dynamic Arraystd::pair — Grouping Two Valuesstd::tuple — Grouping Three or More Values (D1)String STL Methods (Advanced)Iterators — The Bridge Between Containers and AlgorithmsUtility Algorithms
Share:
Vectors, Pairs & Utility Algorithms
std::vector is the workhorse container of competitive programming. Combined with pairs, tuples, and STL algorithms, it gives you the tools to solve most problems elegantly.
std::vector — The Dynamic Array
Unlike C-style arrays, vectors resize dynamically:
hljs cpp
vector<int> v; // empty
vector<int> v(5); // 5 elements, all 0
vector<int> v(5, 10); // 5 elements, all 10
vector<int> v = {1, 2, 3}; // initializer list
Common Operations
hljs cpp
v.push_back(x); // add to end — O(1) amortized
v.pop_back(); // remove from end — O(1)
v.size(); // number of elements
v.empty(); // true if size is 0
v.clear(); // remove all elements
v.front(); // first element
v.back(); // last element
v[i]; // access by index (no bounds check)
v.at(i); // access by index (throws if out of bounds)
Iterators
hljs cpp
v.begin(); // iterator to first element
v.end(); // iterator past last element
v.rbegin(); // reverse iterator to last element
v.rend(); // reverse iterator past first element
2D Vectors
hljs cpp
// n x m grid initialized to 0
vector<vector<int>> grid(n, vector<int>(m, 0));
// Jagged 2D vector
vector<vector<int>> adj(n); // adjacency list
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::pair — Grouping Two Values
hljs cpp
pair<int, int> p = {3, 4};
cout << p.first << " " << p.second; // 3 4
// Nested pairs
pair<int, pair<int, int>> nested = {1, {2, 3}};
cout << nested.second.first; // 2
Pair Comparison
Pairs compare lexicographically: first by .first, then by .second:
hljs cpp
pair<int,int> a = {1, 5};
pair<int,int> b = {1, 3};
a < b; // false (same first, 5 > 3)
pair<int,int> c = {2, 1};
a < c; // true (1 < 2)
This makes pairs naturally sortable.
std::tuple — Grouping Three or More Values
hljs cpp
tuple<int, string, double> t = {1, "hello", 3.14};
// Access
cout << get<0>(t) << " " << get<1>(t); // 1 hello
// Structured binding (C++17)
auto [id, name, score] = t;
Tuple Comparison
Tuples compare lexicographically, like pairs:
hljs cpp
tuple<int,int,int> a = {1, 2, 3};
tuple<int,int,int> b = {1, 2, 4};
a < b; // true
String STL Methods (Advanced)
hljs cpp
string s = "hello world";
// Sorting characters
sort(s.begin(), s.end()); // " dehllloorw"
// Reversing
reverse(s.begin(), s.end()); // "dlrow olleh"
// Removing duplicates (must sort first)
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
// Counting
int count = std::count(s.begin(), s.end(), 'l'); // 3
// Finding
auto it = std::find(s.begin(), s.end(), 'w');
if (it != s.end()) {
int index = it - s.begin();
}
Iterators — The Bridge Between Containers and Algorithms
Iterators are generic pointers that work across all containers:
hljs cpp
// Forward iteration
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
// Reverse iteration
for (auto it = v.rbegin(); it != v.rend(); ++it) {
cout << *it << " ";
}
// With structured bindings
for (auto it = v.begin(); it != v.end(); ++it) {
int index = it - v.begin();
int value = *it;
}
Utility Algorithms
All from <algorithm>:
hljs cpp
// Sorting
sort(v.begin(), v.end());
sort(v.begin(), v.end(), greater<int>()); // descending
// Binary search (on sorted range)
binary_search(v.begin(), v.end(), x); // true/false
auto it = lower_bound(v.begin(), v.end(), x); // first >= x
auto it = upper_bound(v.begin(), v.end(), x); // first > x
// Min/Max
int mn = *min_element(v.begin(), v.end());
int mx = *max_element(v.begin(), v.end());
auto [minIt, maxIt] = minmax_element(v.begin(), v.end());
// Reversing
reverse(v.begin(), v.end());
// Rotating
rotate(v.begin(), v.begin() + k, v.end()); // rotate left by k
// Permutations
next_permutation(v.begin(), v.end()); // next lexicographic permutation
// Accumulate
long long sum = accumulate(v.begin(), v.end(), 0LL);
// Unique (on sorted range, removes consecutive duplicates)
auto last = unique(v.begin(), v.end());
v.erase(last, v.end());
// Fill
fill(v.begin(), v.end(), 0);
// Count
int cnt = count(v.begin(), v.end(), x);
// Find
auto it = find(v.begin(), v.end(), x);
Summary
vectoris the default container — dynamic, cache-friendly, O(1) push_backpaircompares lexicographically by default — useful for sortingtupleextends pairs to 3+ values with the same lexicographic ordering- STL algorithms (
sort,lower_bound,accumulate, etc.) work on any container via iterators - Always pass vectors by
const &to avoid copies
Prefer reading offline? Download the full book as a PDF.
Download Free Book