A range adaptor that represents
view
of elements from an underlying sequence, beginning at the first element for which the predicate returns
false.
drop_while_view models the concepts contiguous_range
, random_access_range
, bidirectional_range
, forward_range
, input_range
, and common_range
when the underlying view V models respective concepts. It also models sized_range
if ranges::forward_range<V> and std::sized_sentinel_for<ranges::sentinel_t<V>, ranges::iterator_t<V>> are modeled.
V
base_
(private) the underlying view
copyable-box
<Pred>
(until C++23)movable-box
<Pred>
(since C++23) pred_
(private) the underlying function object
cache_
(private)
V
satisfies forward_range
) an object that caches the result of begin()
drop_while_view
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] gets the address of derived view's data, provided only if its iterator type satisfies contiguous_iterator
std::ranges::view_interface<D>
) [edit] returns the number of elements in the derived view. Provided if it satisfies forward_range
and its sentinel and iterator type satisfy sized_sentinel_for
.
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range
and common_range
std::ranges::view_interface<D>
) [edit] returns the n
th element in the derived view, provided only if it satisfies random_access_range
std::ranges::view_interface<D>
) [edit] [edit] Deduction guides [edit] Helper templates (since C++20)
This specialization of std::ranges::enable_borrowed_range makes drop_while_view
satisfy borrowed_range
when the underlying view satisfies it.
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'; }
Output:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3494 C++20drop_while_view
was never a borrowed_range
it is a borrowed_range
if its underlying view is [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4