std::transform applies a unary or binary operation to input elements and stores the results in an output range. It is useful for converting data without writing an explicit element-by-element loop. section 9.1.4
The algorithm supports several forms:
- The iterator form takes an input range, an output iterator, and a unary function. section 9.1.4
- A binary form combines two input ranges with a binary function to produce output. section 9.1.4
- C++20 adds a ranges form that accepts an input source instead of an iterator pair; execution-policy overloads also exist. section 9.1.4
Lowercasing a string
A string can be transformed in place with a lambda and std::tolower: std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });. Using unsigned char avoids undefined behavior for values that are not representable as unsigned char or EOF. This normalizes dictionary keys before comparison. section 7.3.1
Marking odd and even numbers
The ranges form converts triangle numbers into display markers: std::ranges::transform(triangle_numbers, std::back_inserter(odd_or_even), [](int i) { return i % 2 ? '.' : '*'; });. The resulting vector can be printed as a compact pattern. section 9.1.4
