From cppreference.com
Defined in header
template<ranges::viewV,classPred>requiresranges::input_range<V>&&std::is_object_v<Pred>&&std::indirect_unary_predicate<constPred,ranges::iterator_t<V>>classdrop_while_view:publicranges::view_interface<drop_while_view<V,Pred>> (1) (since C++20)namespaceviews{inlineconstexpr/* unspecified */drop_while=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR,classPred>requires/* see below */constexprranges::viewautodrop_while(R&&r,Pred&&pred);(since C++20)template<classPred>constexpr/*range adaptor closure*/drop_while(Pred&&pred);(since C++20)1) A range adaptor that represents
of elements from an underlying sequence, beginning at the first element for which the predicate returns false.
drop_while_view models the concepts
,
,
,
,
, and
when the underlying view V models respective concepts. It also models
if ranges::forward_range<V> and std::sized_sentinel_for<ranges::sentinel_t<V>,ranges::iterator_t<V>> are modeled.
Data members
Member Description Vbase_(private)the underlying view
(exposition-only member object*)pred_(private)the underlying function object
(exposition-only member object*)
<ranges::iterator_t<V>>cache_(private)
(present only if V satisfies
)an object that caches the result of
(exposition-only member object*)Member functions
constructs a drop_while_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns a reference to the stored predicate
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
gets the address of derived view's data, provided only if its iterator type satisfies
(public member function of std::ranges::view_interface<D>)
returns the number of elements in the derived view. Provided if it satisfies
and its sentinel and iterator type satisfy
.
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
Helper templates
template<classT,classPred>constexprboolenable_borrowed_range<std::ranges::drop_while_view<T,Pred>>=ranges::enable_borrowed_range<T>;(since C++20)This specialization of std::ranges::enable_borrowed_range makes drop_while_view satisfy
when the underlying view satisfies it.
Notes
In order to provide the amortized constant time complexity required by the
concept, the result of
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.
Example
Run this code
#include<iostream>#include<ranges>#include<string>#include<string_view>usingstd::operator""sv;[[nodiscard]]constexprboolis_space(charp)noexcept{autone=[p](autoq){returnp!=q;};return!!(" \t\n\v\r\f"|std::views::drop_while(ne));};[[nodiscard("trims the output")]]constexprstd::string_viewtrim_left(std::string_viewconstin)noexcept{autoview=in|std::views::drop_while(is_space);return{view.begin(),view.end()};}[[nodiscard("trims the output")]]constexprstd::stringtrim(std::string_viewconstin){autoview=in|std::views::drop_while(is_space)|std::views::reverse|std::views::drop_while(is_space)|std::views::reverse;return{view.begin(),view.end()};}intmain(){static_assert(trim_left(" \n C++23")=="C++23"sv);constexprautosrc{" \f\n\t\r\vHello, C++20!\f\n\t\r\v "sv};static_assert(trim(src)=="Hello, C++20!");staticconstexprautov={0,1,2,3,4,5};for(intn:v|std::views::drop_while([](inti){returni<3;}))std::cout<<n<<' ';std::cout<<'\n';}Output:
3 4 5 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 drop_while_view was never a borrowed_rangeit is a borrowed_range if its underlying view is See also