views::transform
C++23Apply a function to each element lazily.
Section hub
template< ranges::view V, class Pred >
requires ranges::input_range<V> &&
std::is_object_v<Pred> &&
std::indirect_unary_predicate<const Pred, ranges::iterator_t<V>>
class drop_while_view
: public ranges::view_interface<drop_while_view<V, Pred>>
(since C++20)
namespace views {
inline constexpr /* unspecified */ drop_while = /* unspecified */;
}
(since C++20)
Call signature
template< ranges::viewable_range R, class Pred >
requires /* see below */
constexpr ranges::view auto drop_while( R&& r, Pred&& pred );
(since C++20)
template< class Pred >
constexpr /*range adaptor closure*/ drop_while( Pred&& pred );
(since C++20)
In order to provide the amortized constant time complexity required by the range concept, the result of begin is cached within the drop_while_view object. If the underlying range is modified after the first call to begin(), subsequent uses of the drop_while_view object might have unintuitive behavior.
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
using std::operator""sv;
[[nodiscard]]
constexpr bool is_space(char p) noexcept
{
auto ne = [p](auto q) { return p != q; };
return !!(" \t\n\v\r\f" | std::views::drop_while(ne));
};
[[nodiscard("trims the output")]]
constexpr std::string_view trim_left(std::string_view const in) noexcept
{
auto view = in | std::views::drop_while(is_space);
return {view.begin(), view.end()};
}
[[nodiscard("trims the output")]]
constexpr std::string trim(std::string_view const in)
{
auto view = in
| std::views::drop_while(is_space)
| std::views::reverse
| std::views::drop_while(is_space)
| std::views::reverse
;
return {view.begin(), view.end()};
}
int main()
{
static_assert(trim_left(" \n C++23") == "C++23"sv);
constexpr auto src{" \f\n\t\r\vHello, C++20!\f\n\t\r\v "sv};
static_assert(trim(src) == "Hello, C++20!");
static constexpr auto v = {0, 1, 2, 3, 4, 5};
for (int n : v | std::views::drop_while([](int i) { return i < 3; }))
std::cout << n << ' ';
std::cout << '\n';
}
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3494 | C++20 | drop_while_view was never a borrowed_range | it is a borrowed_range if its underlying view is |
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.