|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
template<ranges::view V,class Pred> requiresranges::input_range<V>&& | (1) | (since C++20) |
namespace views{ inlineconstexpr/* unspecified */ drop_while=/* unspecified */; | (2) | (since C++20) |
Call signature | ||
template<ranges::viewable_range R,class Pred> requires/* see below */ | (since C++20) | |
template<class Pred> constexpr/*range adaptor closure*/ drop_while( Pred&& pred); | (since C++20) | |
view
of elements from an underlying sequence, beginning at the first element for which the predicate returnsfalse.drop_while_view models the conceptscontiguous_range
,random_access_range
,bidirectional_range
,forward_range
,input_range
, andcommon_range
when the underlying viewV models respective concepts. It also modelssized_range
ifranges::forward_range<V> andstd::sized_sentinel_for<ranges::sentinel_t<V>,ranges::iterator_t<V>> are modeled.
Contents |
Member | Description |
V base_ (private) | the underlying view (exposition-only member object*) |
copyable-box <Pred> (until C++23)movable-box <Pred> (since C++23)pred_ (private) | the underlying function object (exposition-only member object*) |
non-propagating-cache<ranges::iterator_t<V>>cache_ (private)(present only if V satisfiesforward_range ) | an object that caches the result ofbegin() (exposition-only member object*) |
constructs adrop_while_view (public member function)[edit] | |
returns a copy of the underlying (adapted) view (public member function)[edit] | |
returns a reference to the stored predicate (public member function)[edit] | |
returns an iterator to the beginning (public member function)[edit] | |
returns an iterator or a sentinel to the end (public member function)[edit] | |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D> )[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D> )[edit] |
returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D> )[edit] | |
gets the address of derived view's data, provided only if its iterator type satisfiescontiguous_iterator (public member function of std::ranges::view_interface<D> )[edit] | |
returns the number of elements in the derived view. Provided if it satisfiesforward_range and its sentinel and iterator type satisfysized_sentinel_for .(public member function of std::ranges::view_interface<D> )[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns then th element in the derived view, provided only if it satisfiesrandom_access_range (public member function of std::ranges::view_interface<D> )[edit] |
template<class T,class Pred> constexprbool enable_borrowed_range<std::ranges::drop_while_view<T, Pred>>= | (since C++20) | |
This specialization ofstd::ranges::enable_borrowed_range makesdrop_while_view
satisfyborrowed_range
when the underlying view satisfies it.
In order to provide the amortized constant time complexity required by therange
concept, the result ofbegin
is cached within thedrop_while_view
object. If the underlying range is modified after the first call tobegin(), subsequent uses of thedrop_while_view
object might have unintuitive behavior.
#include <iostream>#include <ranges>#include <string>#include <string_view> using std::operator""sv; [[nodiscard]]constexprbool 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")]]constexprstd::string_view trim_left(std::string_viewconst in)noexcept{auto view= in| std::views::drop_while(is_space);return{view.begin(), view.end()};} [[nodiscard("trims the output")]]constexprstd::string trim(std::string_viewconst 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); constexprauto src{"\f\n\t\r\vHello, C++20!\f\n\t\r\v "sv}; static_assert(trim(src)=="Hello, C++20!"); staticconstexprauto 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:
3 4 5
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3494 | C++20 | drop_while_view was never aborrowed_range | it is aborrowed_range if its underlying view is |
(C++20) | aview consisting of elements of anotherview , skipping the first N elements(class template)(range adaptor object)[edit] |