title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
take_while_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> take_while_view class, which is a view that contains the leading elements of a range that match a predicate. |
08/04/2022 |
|
|
|
A view that contains the leading elements of a range that match a predicate.
template<view V, class Pred> requires
input_range<V> && is_object_v<Pred> &&
indirect_unary_predicate<const Pred, iterator_t<V>>
class take_while_view : public view_interface<take_while_view<V, Pred>>;
Pred
The type of the predicate that determines the leading elements to put in the view.
V
The type of the underlying view.
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::take_while |
Underlying range | Must satisfy input_range or higher |
Element type | Same as the underlying range |
View iterator category | Same as the underlying range |
Sized | No |
Is const -iterable |
Only if the underlying range is const iterable and the predicate can work with const references. |
Common range | No |
Borrowed range | No |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct the view. |
base C++20 |
Get the underlying range. |
begin C++20 |
Get an iterator to the first element. |
end C++20 |
Get the sentinel at the end of the view. |
pred C++20 |
Get a reference to the predicate that determines which elements to take. |
Inherited from view_interface |
Description |
back C++20 |
Get the last element. |
data C++20 |
Get a pointer to the first element. |
empty C++20 |
Test whether the view is empty. |
front C++20 |
Get the first element. |
operator[] C++20 |
Get the element at the specified position. |
operator bool C++20 |
Test whether the view isn't empty. |
size |
Get the number of elements in the view. |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Construct an instance of a take_while_view
1) take_while_view() requires
default_initializable<V> &&
default_initializable<Pred> = default;
2) constexpr take_while_view(V base, Pred pred);
base
The underlying view.
pred
The predicate that determines the leading elements to put in the view.
For information about template parameter types, see Template parameters.
A take_while_view
object.
The best way to create a take_while_view
is by using the views::take_while
range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.
1) Move constructs the take_while_view
from a base
view and a pred
predicate. Both base
and pred
are moved via std::move()
.
2) Constructs an empty take_while_view
. The underlying view and predicate are default constructed.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{0, 1, 2, 3, -4, 5, 6};
auto twv = std::views::take_while(v, [](int i) {return i >= 0; });
for (auto& e : twv)
{
std::cout << e << ' '; // 0 1 2 3
}
std::cout << '\n';
// Using the '|' operator to create a take_view
for (auto i : v | std::views::take_while([](int i) {return i < 5; }))
{
std::cout << i << ' '; // 0 1 2 3 -4
}
}
0 1 2 3
0 1 2 3 -4
Gets a copy of the underlying view.
// Uses a copy constructor to return the underlying view
1) constexpr V base() const& requires std::copy_constructible<V>;
// Uses a move constructor to return the underlying view
2) constexpr V base() &&;
None.
A copy of the underlying view.
Get an iterator to the first element in the view.
1) constexpr auto begin() requires (!Simple_view<V>);
2) constexpr auto begin() const requires
range<const V> &&
indirect_unary_predicate<const Pred, iterator_t<const V>>
None.
An iterator pointing at the first element in the view. The behavior is undefined if the view doesn't have a predicate.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().":::
For 1, the Simple_view
requirement means that a view V
and const V
have the same iterator and sentinel types.
Get the sentinel at the end of the view.
1) constexpr auto end() requires (!Simple_view<V>);
2) constexpr auto end() const requires
range<const V> &&
indirect_unary_predicate<const Pred, iterator_t<const V>
None.
The sentinel that follows the last element in the view.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().":::
For 1, the Simple_view
requirement means that a view V
and const V
have the same iterator and sentinel types.
Get a reference to the predicate used to select which leading elements will go in the view.
constexpr const Pred& pred() const;
A reference to the predicate used to select the leading elements to put in the view.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{ 0, 1, 2, 3, -4, 5, 6 };
auto mv = v | std::views::take_while(
[](int i) {return i < 5; });
std::cout << std::boolalpha << mv.pred()(v[6]); // outputs false because v[6] = 6 and 6 is not less than 5 (the predicate)
}