Scroll to navigation

std::ranges::views::take_while,std::ranges::take_while_view(3) C++ Standard Libary std::ranges::views::take_while,std::ranges::take_while_view(3)

NAME

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

Synopsis


Defined in header <ranges>
template< ranges::view V, class Pred >


requires ranges::input_range<V> &&
std::is_object_v<Pred> && (1) (since C++20)
std::indirect_unary_predicate<const Pred, ranges::iterator_t<V>>


class take_while_view : public
ranges::view_interface<take_while_view<V, Pred>>
namespace views {


inline constexpr /*unspecified*/ take_while = /*unspecified*/; (2) (since C++20)


}
Call signature
template< ranges::viewable_range R, class Pred >


requires /* see below */ (since C++20)


constexpr ranges::view auto take_while( R&& r, Pred&& pred );
template< class Pred > (since C++20)
constexpr /*range adaptor closure*/ take_while( Pred&& pred );


1) A range adaptor that represents view of the elements from an underlying sequence,
starting at the beginning and ending at the first element for which the predicate
returns false.
2) Range adaptor object. 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.


Expression-equivalent


Expression e is expression-equivalent to expression f, if


* e and f have the same effects, and
* either both are constant subexpressions or else neither is a constant
subexpression, and
* either both are potentially-throwing or else neither is potentially-throwing
(i.e. noexcept(e) == noexcept(f)).

Member functions


constructor constructs a take_while_view
(C++20) (public member function)
base returns a copy of the underlying (adapted) view
(C++20) (public member function)
pred returns a reference to the stored predicate
(C++20) (public member function)
begin returns an iterator to the beginning
(C++20) (public member function)
end returns a sentinel representing the end
(C++20) (public member function)
Inherited from std::ranges::view_interface
empty Returns whether the derived view is empty. Provided if it satisfies
(C++20) sized_range or forward_range.
(public member function of std::ranges::view_interface<D>)
operator bool Returns whether the derived view is not empty. Provided if
(C++20) ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>)
data Gets the address of derived view's data. Provided if its iterator type
(C++20) satisfies contiguous_iterator.
(public member function of std::ranges::view_interface<D>)
front Returns the first element in the derived view. Provided if it
(C++20) satisfies forward_range.
(public member function of std::ranges::view_interface<D>)
operator[] Returns the nth element in the derived view. Provided if it satisfies
(C++20) random_access_range.
(public member function of std::ranges::view_interface<D>)


Deduction guides


Nested classes


sentinel the sentinel type
(exposition-only member class template)

Example

// Run this code


#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';
}

Output:


2017 2018 2019 2020
Today is yesterday's tomorrow!

See also


ranges::take_view a view consisting of the first N elements of another view
views::take (class template) (range adaptor object)
(C++20)
ranges::drop_while_view a view consisting of the elements of another view, skipping
views::drop_while the initial subsequence of elements until the first element
(C++20) where the predicate returns false
(class template) (range adaptor object)

2022.07.31 http://cppreference.com