Do more with less code: the <algorithm> library (sort, find, transform, accumulate) driven by lambdas, the erase-remove idiom, and a first look at ranges.
The algorithms library expresses common operations once, correctly, over any range. Pass a lambda to customize behavior — a comparator for sort, a predicate for find_if, a mapping for transform. Reaching for these instead of hand-written loops makes code shorter and clearer.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
int main() {
std::vector<int> v{5, 2, 8, 1, 9};
std::sort(v.begin(), v.end(),
[](int a, int b) { return a > b; }); // descending
auto it = std::find_if(v.begin(), v.end(),
[](int x) { return x < 3; });
if (it != v.end()) std::cout << "first < 3: " << *it << "\n"; // 2
int sum = std::accumulate(v.begin(), v.end(), 0);
std::cout << "sum: " << sum << "\n"; // 25
return 0;
}Algorithms like std::remove don’t actually shrink a container — they shuffle the kept elements to the front and return an iterator to the new logical end. Pairing it with erase() truly removes the leftovers. C++20 adds std::erase / std::erase_if to do both in one call.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v{1, 2, 3, 2, 4, 2};
// classic erase-remove: drop every 2
v.erase(std::remove(v.begin(), v.end(), 2), v.end());
// C++20 shorthand for the same thing:
// std::erase(v, 2);
for (int x : v) std::cout << x << ' '; // 1 3 4
std::cout << "\n";
return 0;
}C++20 ranges let you compose algorithms into readable pipelines with the | operator, and they act on whole containers instead of begin/end pairs. Views like filter and transform are lazy — no intermediate containers are created.
#include <iostream>
#include <vector>
#include <ranges>
int main() {
std::vector<int> v{1, 2, 3, 4, 5, 6};
auto even_squares = v
| std::views::filter([](int x) { return x % 2 == 0; })
| std::views::transform([](int x) { return x * x; });
for (int x : even_squares) std::cout << x << ' '; // 4 16 36
std::cout << "\n";
return 0;
}