std::ranges::views::take_while, std::ranges::take_while_view
<ranges>
requires ranges::input_range <V> &&
std::is_object_v <Pred> &&
std::indirect_unary_predicate <const Pred, ranges::iterator_t <V>>
class take_while_view
inline constexpr /*unspecified*/ take_while = /*unspecified*/;
requires /* see below */
constexpr /*range adaptor closure*/ take_while( Pred&& pred );
view
of the elements from an underlying sequence, starting at the beginning and ending at the first element for which the predicate returns false.take_while_view
models the concepts contiguous_range
, random_access_range
, bidirectional_range
, forward_range
, and input_range
when the underlying view V
models respective concepts.
Contents
[edit] Data members
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*)
[edit] Member functions
Inherited from std::ranges::view_interface
sized_range
or forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
contiguous_iterator
(public member function of
std::ranges::view_interface<D>
) [edit]
forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
n
th element in the derived view, provided only if it satisfies random_access_range
(public member function of
std::ranges::view_interface<D>
) [edit]
[edit] Deduction guides
[edit] Nested classes
[edit] Notes
For forward_iterator
s, views::take_while(v, pred) is similar to ranges::subrange {ranges::begin (v), ranges::find_if_not (v, pred)}, but the latter invokes pred only during construction (while the former invokes pred each time a valid take_while
iterator is compared to a sentinel).
[edit] Example
#include <iostream> #include <ranges> int main() { for (int year : std::views::iota (2020) | std::views::take_while([](int y){ return y < 2026; })) std::cout << year << ' '; std::cout << '\n'; const char note[]{"Today is yesterday's tomorrow!..."}; auto not_dot = [](char c){ return c != '.'; }; for (char x : std::ranges::take_while_view(note, not_dot)) std::cout << x; std::cout << '\n'; }
Output:
2020 2021 2022 2023 2024 2025 Today is yesterday's tomorrow!
[edit] See also
view
consisting of the first N elements of another view
(class template) (range adaptor object)[edit]