liveBook · Manning

Transform

As explained in Learn C++ by Example

std::transform is a C++ standard-library algorithm that applies a unary or binary operation to input elements and stores the resulting values in an output range.

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

How Learn C++ by Example covers it

How deep it goes
Explains unary, binary, ranges, and execution-policy forms with working examples, but defers detailed implementation internals.
Examples
Demonstrates lowercase strings, dictionary loading, triangle-number parity markers, and vector output.
What it assumes
Assumes basic C++ strings, containers, lambdas, iterators, and standard-library headers.
What you can do after
Apply it via the C++ standard library to convert ranges in place or append transformed results to an output container.
How it connects
Connects transformations with lambda expressions, iterators, ranges, output iterators, and execution policies.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.