std::ranges::views::take_while, std::ranges::take_while_view

< cpp‎ | ranges
template< ranges::view V, class Pred >

    requires ranges::input_range<V> &&
             std::is_object_v<Pred> &&
             std::indirect_unary_predicate<const Pred, ranges::iterator_t<V>>

class take_while_view : public ranges::view_interface<take_while_view<V, Pred>>
(1) (since C++20)
namespace views {

    inline constexpr /*unspecified*/ take_while = /*unspecified*/;

(2) (since C++20)
1) A range adaptor that represents view of the first N elements from an underlying sequence, ending before the first element for which the predicate returns false.
2) The expression views::take_while(E, F) is expression-equivalent to take_while_view{E, F} for any suitable subexpressions E and F.

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.


[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

constructs a take_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 a sentinel representing the end
(public member function) [edit]

[edit] Deduction guides

[edit] Nested classes

the sentinel type
(public member class)

[edit] Example

#include <ranges>
#include <iostream>
int main()
    for (int year : std::views::iota(2017)
                  | std::views::take_while([](int y) { return y <= 2020; })) {
        std::cout << year << ' ';
    std::cout << '\n';
    const char idea[] {"Today is yesterday's tomorrow!.."};
    for (char x : std::ranges::take_while_view(idea, [](char c) { return c != '.'; })) {
        std::cout << x;
    std::cout << '\n';


2017 2018 2019 2020 
Today is yesterday's tomorrow!