Transforming, Accumulating & Removing
Transforming
std::transform(in.begin(), in.end(), out.begin(), [](double p) { return p * 1.13; });
std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::plus{}); // two inputs
The output must have room — or use std::back_inserter(out).
Numeric algorithms (<numeric>)
| Algorithm | Does |
|---|---|
std::accumulate(b, e, init) | sum (or fold with any operation) |
std::reduce | like accumulate, may run in any order (parallelizable) |
std::partial_sum / std::inclusive_scan | running totals |
std::inner_product / std::transform_reduce | dot product, weighted sums |
std::iota(b, e, start) | fill with start, start+1, … |
std::adjacent_difference | differences between neighbours |
⚠️ accumulate's result type is the type of init: accumulate(v.begin(), v.end(), 0) on doubles truncates! Use 0.0.
Copying and filling
std::copy, std::copy_if, std::fill, std::generate, std::replace, std::replace_if, std::unique, std::rotate, std::shuffle
Removing: the erase–remove idiom
std::remove/std::remove_if don't shrink the container — they move the kept elements forward and return the new logical end:
v.erase(std::remove_if(v.begin(), v.end(), pred), v.end()); // classic idiom
std::erase_if(v, pred); // C++20 ✅
Same for std::unique (removes consecutive duplicates): v.erase(std::unique(v.begin(), v.end()), v.end()); after sorting → deduplicate.
Partitioning
std::partition / std::stable_partition move matching elements to the front (e.g. paid bills before unpaid).
Why prefer algorithms over raw loops?
- The name says what happens (
count_ifvs. a loop with a counter) - Fewer off-by-one bugs
- Some can run in parallel:
std::sort(std::execution::par, ...)(<execution>)
Example
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
template <typename C>
void print(const char* label, const C& c) {
std::cout << label << ':';
for (const auto& x : c) std::cout << ' ' << x;
std::cout << '\n';
}
int main() {
std::vector<double> prices = { 100, 250, 1500, 49.5 };
std::vector<double> with_vat(prices.size());
std::transform(prices.begin(), prices.end(), with_vat.begin(), [](double p) { return p * 1.13; });
print("with VAT", with_vat);
std::cout << "accumulate with 0 = " << std::accumulate(prices.begin(), prices.end(), 0) << " (truncated!)\n";
std::cout << "accumulate with 0.0 = " << std::accumulate(prices.begin(), prices.end(), 0.0) << '\n';
std::vector<int> qty = { 2, 1, 3, 4 };
double bill = std::inner_product(prices.begin(), prices.end(), qty.begin(), 0.0); // sum of price * qty
std::cout << "bill (price x qty) = " << bill << '\n';
std::vector<int> daily = { 500, -200, 1000, -350, 50 };
std::vector<int> running;
std::partial_sum(daily.begin(), daily.end(), std::back_inserter(running));
print("running balance", running);
std::vector<int> ids(5);
std::iota(ids.begin(), ids.end(), 1001);
print("ids", ids);
std::vector<int> entries = { 300, 0, 450, -20, 0, 1200 };
auto new_end = std::remove_if(entries.begin(), entries.end(), [](int x) { return x <= 0; });
std::cout << "after remove_if, size is still " << entries.size() << '\n';
entries.erase(new_end, entries.end()); // erase-remove idiom
print("valid entries", entries);
std::vector<int> tags = { 3, 1, 3, 2, 1, 3 };
std::sort(tags.begin(), tags.end());
tags.erase(std::unique(tags.begin(), tags.end()), tags.end()); // sort + unique = deduplicate
print("unique tags", tags);
std::vector<int> big;
std::copy_if(daily.begin(), daily.end(), std::back_inserter(big), [](int x) { return x >= 500; });
print("big days", big);
std::vector<int> bills = { 1, 0, 1, 1, 0 }; // 1 = paid
std::stable_partition(bills.begin(), bills.end(), [](int b) { return b == 1; });
print("paid first", bills);
return 0;
}
/* Output:
with VAT: 113 282.5 1695 55.935
accumulate with 0 = 1899 (truncated!)
accumulate with 0.0 = 1899.5
bill (price x qty) = 5148
running balance: 500 300 1300 950 1000
ids: 1001 1002 1003 1004 1005
after remove_if, size is still 6
valid entries: 300 450 1200
unique tags: 1 2 3
big days: 500 1000
paid first: 1 1 1 0 0
*/