views::transform
C++23Apply a function to each element lazily.
Section hub
template<
std::input_or_output_iterator I,
std::sentinel_for<I> S = I,
ranges::subrange_kind K = std::sized_sentinel_for<S, I> ?
ranges::subrange_kind::sized :
ranges::subrange_kind::unsized
>
requires (K == ranges::subrange_kind::sized ||
!std::sized_sentinel_for<S, I>)
class subrange
: public ranges::view_interface<subrange<I, S, K>>
(since C++20)
Helper concepts
template<class From, class To>
concept /*uses-nonqualification-pointer-conversion*/ =
/* see description */;
(exposition only*)
template<class From, class To>
concept /*convertible-to-non-slicing*/ = /* see description */;
(exposition only*)
#include <map>
#include <print>
#include <ranges>
void make_uppercase(char& v)
{
v += 'A' - 'a';
}
void uppercase_transform(std::multimap<int, char>& m, int k)
{
auto [first, last] = m.equal_range(k);
for (auto& [_, v] : std::ranges::subrange(first, last))
make_uppercase(v);
}
int main()
{
std::multimap<int, char> mm{{4, 'a'}, {3, '-'}, {4, 'b'}, {5, '-'}, {4, 'c'}};
std::println("Before: {}", mm);
uppercase_transform(mm, 4);
std::println("After: {}", mm);
}
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3470 | C++20 | convertible-to-non-slicing might reject qualification conversions | always accepts them |
This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.
Start here for the adapters most people reach for first when building pipelines.
Apply a function to each element lazily.
Keep only elements that satisfy a predicate.
Keep the first N elements from a source range.
Skip the first N elements and expose the rest.
Split a range into non-overlapping fixed-size subranges.
Flatten a range of ranges into a single lazy sequence.
These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.
Normalize a range into a view-compatible form.
Adapt iterator/sentinel pairs into a common-range shape.
Wrap an existing range by reference.
Store and expose a range with unique ownership.
Package iterator + sentinel as a view-like object.
Project tuple-like elements to their key component.
Project tuple-like elements to their value component.
Newer adapters, kept as a compact scan list with only standard badges.
A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.