title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
take_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> take_view class. It's a view that contains the specified number of elements taken from the front of another view. |
10/19/2022 |
|
|
|
A view of the first N elements from another view.
template<view V>
class take_view : public view_interface<take_view<V>>;
V
The type of the underlying range.
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::take |
Underlying range | Any range |
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 |
Common range | Only if the underlying range satisfies random_access_range and sized_range |
Borrowed range | Only if the underlying range is borrowed_range |
Member functions | Description |
---|---|
Constructors | 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. |
size C++20 |
Get the number of elements. The underlying range must satisfy sized_range . |
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. |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Construct an instance of a take_view
1) take_view() requires default_initializable<V> = default;
2) constexpr take_view(V base, range_difference_t<V> count);
base
The underlying view.
count
The number of elements to take from the front of the underlying view. If count
is more than the number of elements in the underlying view, the view will contain all the elements in the underlying range.
For information about the template parameter type, see Template parameters.
A take_view
, which is a view of the first N elements from another view. If you specify more elements to drop than exist in the underlying range, an empty_view
is returned.
The best way to create a take_view
is by using the views::take
range adaptor. Range adaptors are the intended way to create view classes. The view types are only exposed in case you want to create your own custom view type.
1) Create a take_view
that has no elements. The underlying view is default constructed. base()
returns a copy of V()
.
2) Create a take_view
from a base
and a count. base
is moved via std::move()
.
If count
is less than the number of elements in the underlying range, then count
determines the size of the take_view
.
If count
is greater than the number of elements in the underlying range, then the take_view
includes all of the elements in the underlying range.
// 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 newView = std::views::take(v, 3);
for (auto& e : newView)
{
std::cout << e << ' '; // 0 1 2
}
std::cout << '\n';
// Use the '|' operator to create a take_view
for (auto i : v | std::views::take(3))
{
std::cout << i << ' '; // 0 1 2
}
}
0 1 2
0 1 2
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.
The underlying view.
Get an iterator to the first element in the view.
constexpr auto begin() requires (!Simple_view<V>);
constexpr auto begin() const requires range<const V>;
None.
An iterator pointing at the first 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<V>
requirement means that the view V
and const V
must 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>;
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<V>
requirement means that the view V
and const V
must have the same iterator and sentinel types.
Get the number of elements.
constexpr auto size() requires sized_range<V>;
constexpr auto size() const requires sized_range<const V>;
None.
The number of elements in the take_view
.
If the take_view
was constructed with an explicit count
:
- if
count
is less than the number of elements in the underlying range, it's returned as the size of the view. - if
count
is greater than the number of elements in the underlying range, then the size of the view isranges::size(base)
.