Files
cppdraft_translate/cppdraft/range/drop.md
2025-10-25 03:02:53 +03:00

9.3 KiB
Raw Blame History

[range.drop]

25 Ranges library [ranges]

25.7 Range adaptors [range.adaptors]

25.7.12 Drop view [range.drop]

25.7.12.1 Overview [range.drop.overview]

1

#

drop_view produces a view excluding the first N elements from another view, or an empty range if the adapted view contains fewer than N elements.

2

#

The name views::drop denotes a range adaptor object ([range.adaptor.object]).

Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>.

If decltype((F)) does not modelconvertible_to,views::drop(E, F) is ill-formed.

Otherwise, the expression views::drop(E, F) is expression-equivalent to:

a specialization of span ([views.span]),

a specialization of basic_string_view ([string.view]),

a specialization of iota_view ([range.iota.view]), or

a specialization of subrange ([range.subrange]) where T::StoreSize is false,

then U(ranges::begin(E) + std::min(ranges::distance(E), F), ranges::end(E)), except that E is evaluated only once, where U is span<typename T::element_type> if T is a specialization of span and T otherwise.

  • (2.3)

    Otherwise, if T is a specialization of subrange that models random_access_range and sized_range, thenT(ranges::begin(E) + std::min(ranges::distance(E), F), ranges::end(E),to-unsigned-like(ranges::distance(E) - std::min(ranges::distance(E), F))), except that E and F are each evaluated only once.

  • (2.4)

    Otherwise, if T is a specialization of repeat_view ([range.repeat.view]):

if T models sized_range, thenviews::repeat(*E.value_, ranges::distance(E) - std::min(ranges::distance(E), F)) except that E is evaluated only once;

otherwise, ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.

  • (2.5)

    Otherwise, drop_view(E, F).

3

#

[Example 1: auto ints = views::iota(0) | views::take(10);for (auto i : ints | views::drop(5)) { cout << i << ' '; // prints 5 6 7 8 9} — end example]

25.7.12.2 Class template drop_view [range.drop.view]

🔗

namespace std::ranges {template<view V>class drop_view : public view_interface<drop_view> {public: drop_view() requires default_initializable = default; constexpr explicit drop_view(V base, range_difference_t count); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr auto begin()requires (!(simple-view &&random_access_range && sized_range)); constexpr auto begin() constrequires random_access_range && sized_range; constexpr auto end() requires (simple-view){ return ranges::end(base_); }constexpr auto end() const requires range{ return ranges::end(base_); }constexpr auto size() requires sized_range {const auto s = ranges::size(base_); const auto c = static_cast<decltype(s)>(count_); return s < c ? 0 : s - c; }constexpr auto size() const requires sized_range {const auto s = ranges::size(base_); const auto c = static_cast<decltype(s)>(count_); return s < c ? 0 : s - c; }constexpr auto reserve_hint() requires approximately_sized_range {const auto s = static_cast<range_difference_t>(ranges::reserve_hint(base_)); return to-unsigned-like(s < count_ ? 0 : s - count_); }constexpr auto reserve_hint() const requires approximately_sized_range {const auto s = static_cast<range_difference_t>(ranges::reserve_hint(base_)); return to-unsigned-like(s < count_ ? 0 : s - count_); }private: V base_ = V(); // exposition only range_difference_t count_ = 0; // exposition only}; template drop_view(R&&, range_difference_t) -> drop_view<views::all_t>;}

🔗

constexpr explicit drop_view(V base, range_difference_t<V> count);

1

#

Preconditions: count >= 0 is true.

2

#

Effects: Initializes base_ with std::move(base) andcount_ with count.

🔗

constexpr auto begin() requires (!([simple-view](range.utility.helpers#concept:simple-view "25.5.2Helper concepts[range.utility.helpers]")<V> && [random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]")<const V> && [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]")<const V>)); constexpr auto begin() const requires [random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]")<const V> && [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]")<const V>;

3

#

Returns: ranges::next(ranges::begin(base_), count_, ranges::end(base_)).

4

#

Remarks: In order to provide the amortized constant-time complexity required by the range concept when drop_view models forward_range, the first overload caches the result within the drop_view for use on subsequent calls.

[Note 1:

Without this, applying a reverse_view over a drop_view would have quadratic iteration complexity.

— end note]