std::ranges::views::drop_while, std::ranges::drop_while_view
Defined in header <ranges>
|
||
template< ranges::view V, class Pred > requires ranges::input_range<V> && |
(1) | (since C++20) |
namespace views { inline constexpr /*unspecified*/ drop_while = /*unspecified*/; |
(2) | (since C++20) |
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.
Contents |
[edit] Expression-equivalent
Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.
[edit] Member functions
(C++20) |
constructs a drop_while_view (public member function) |
(C++20) |
returns a copy of the underlying (adapted) view (public member function) |
(C++20) |
returns a reference to the stored predicate (public member function) |
(C++20) |
returns an iterator to the beginning (public member function) |
(C++20) |
returns an iterator or a sentinel to the end (public member function) |
Inherited from std::ranges::view_interface | |
(C++20) |
Returns whether the derived view is empty. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
|
(C++20) |
Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> )
|
(C++20) |
Gets the address of derived view's data. Provided if its iterator type satisfies contiguous_iterator . (public member function of std::ranges::view_interface<D> )
|
(C++20) |
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 . (public member function of std::ranges::view_interface<D> )
|
(C++20) |
Returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
|
(C++20) |
Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range . (public member function of std::ranges::view_interface<D> )
|
(C++20) |
Returns the nth element in the derived view. Provided if it satisfies random_access_range . (public member function of std::ranges::view_interface<D> )
|
[edit] Deduction guides
[edit] Helper templates
template<class T, class Pred> inline constexpr bool enable_borrowed_range<std::ranges::drop_while_view<T, Pred>> = |
(since C++20) | |
This specialization of std::ranges::enable_borrowed_range makes drop_while_view
satisfy borrowed_range
when the underlying view satisfies it.
[edit] Notes
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.
[edit] Example
#include <cctype> #include <iomanip> #include <iostream> #include <ranges> #include <string> #include <string_view> std::string trim(std::string_view const in) { auto view = in | std::views::drop_while(isspace) | std::views::reverse | std::views::drop_while(isspace) | std::views::reverse ; return {view.begin(), view.end()}; } int main() { const auto s = trim(" \f\n\t\r\vHello, C++20!\f\n\t\r\v "); std::cout << std::quoted(s) << '\n'; 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 << ' '; } }
Output:
"Hello, C++20!" 3 4 5
[edit] Defect reports
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 a borrowed_range
|
it is a borrowed_range if its underlying view is
|
[edit] See also
(C++20) |
a view consisting of elements of another view , skipping the first N elements (class template) (range adaptor object) |