drop_view class (C++ Standard Library)

Create a view that excludes the first N elements of a range.

Syntax

template<ranges::view V>
class drop_view : public ranges::view_interface<drop_view<V>>;

Template parameters

V
The type of the underlying view.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::drop
Underlying range Must satisfy output_range or higher
Element type Same as the underlying range
View iterator category Same as the underlying range
Sized Only if the underlying range satisfies sized_range
Is const-iterable Only if the underlying range is const iterable and satisfies random_access_range and sized_range
Common range Only if the underlying range is a common_range
Borrowed range Only if the underlying range satisfies borrowed_range

Members

Member functions Description
ConstructorsC++20 Construct a drop_view.
baseC++20 Get the underlying view.
beginC++20 Get an iterator to the first element.
endC++20 Get the sentinel at the end of the view.
sizeC++20 Get the number of elements in this view. The underlying range must satisfy sized_range.
Inherited from view_interface Description
backC++20 Get the last element.
dataC++20 Get a pointer to the first element.
emptyC++20 Test whether the drop_view is empty.
frontC++20 Get the first element.
operator[]C++20 Get the element at the specified position.
operator boolC++20 Test whether the drop_view isn't empty.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Construct an instance of a drop_view

template<ranges::view V>
class drop_view : public ranges::view_interface<drop_view<V>>

Template parameters

V
The type of the underlying view.

Return value

A view of the underlying range, excluding the specified number of elements from the front.
If you specify more elements to drop than exist in the underlying range, then an empty_view is returned.

Remarks

The best way to create a drop_view is by using the views::drop 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.

Example: drop_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 5 };
    auto newView = std::views::drop(v, 3);
    for (auto e : newView) // outputs 4 5
    {
        std::cout << e << ' ';
    }
    std::cout << '\n';

    auto numbers = std::views::iota(0) | std::views::take(10); // generate a view of 10 integers
    for (auto i : numbers | std::views::drop(5)) // use the '|' syntax to create a drop_view
    {
        std::cout << i << ' '; // outputs 5 6 7 8 9
    }
}
4 5
5 6 7 8 9

base

Gets a copy of the underlying view.

// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;

// Uses a move constructor to return the underlying view
constexpr V base() &&;

Parameters

None.

Return value

The underlying view.

begin

Get an iterator to the first element in the drop_view.

constexpr auto begin()
  requires (!(Simple_view<V> && ranges::random_access_range<const V> && ranges::sized_range<const V>));

constexpr auto begin() const
  requires ranges::random_access_range<const V> && ranges::sized_range<const V>;

Parameters

None.

Return value

An iterator pointing at the first element in the drop_view.

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().

end

Get the sentinel at the end of the drop_view

constexpr auto end() requires (!Simple_view<V>);
constexpr auto end() const requires ranges::range<const V>;

Parameters

None.

Return value

The sentinel that follows the last element in the drop_view:

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().

size

Get the number of elements in the drop_view.

constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;

Parameters

None.

Return value

The number of elements in the drop_view.

Remarks

The underlying range must satisfy sized_range.

See also

<ranges>
drop range adaptor
take_while()
take_while_view
View classes