8.2 KiB
[counted.iterator]
24 Iterators library [iterators]
24.5 Iterator adaptors [predef.iterators]
24.5.7 Counted iterators [iterators.counted]
24.5.7.1 Class template counted_iterator [counted.iterator]
Class template counted_iterator is an iterator adaptor with the same behavior as the underlying iterator except that it keeps track of the distance to the end of its range.
It can be used together with default_sentinel in calls to generic algorithms to operate on a range of N elements starting at a given position without needing to know the end position a priori.
[Example 1: list s;// populate the list s with at least 10 strings vector v;// copies 10 strings into v: ranges::copy(counted_iterator(s.begin(), 10), default_sentinel, back_inserter(v)); â end example]
Two values i1 and i2 of typescounted_iterator andcounted_iterator refer to elements of the same sequence if and only if there exists some integer n such thatnext(i1.base(), i1.count() + n) andnext(i2.base(), i2.count() + n) refer to the same (possibly past-the-end) element.
namespace std {template<input_or_output_iterator I>class counted_iterator {public:using iterator_type = I; using value_type = iter_value_t; // present only// if I models indirectly_readableusing difference_type = iter_difference_t; using iterator_concept = typename I::iterator_concept; // present only// if the qualified-id I::iterator_concept is valid and denotes a typeusing iterator_category = typename I::iterator_category; // present only// if the qualified-id I::iterator_category is valid and denotes a typeconstexpr counted_iterator() requires default_initializable = default; constexpr counted_iterator(I x, iter_difference_t n); templaterequires convertible_to<const I2&, I>constexpr counted_iterator(const counted_iterator& x); templaterequires assignable_from<I&, const I2&>constexpr counted_iterator& operator=(const counted_iterator& x); constexpr const I& base() const & noexcept; constexpr I base() &&; constexpr iter_difference_t count() const noexcept; constexpr decltype(auto) operator*(); constexpr decltype(auto) operator*() constrequires dereferenceable; constexpr auto operator->() const noexceptrequires contiguous_iterator; constexpr counted_iterator& operator++(); constexpr decltype(auto) operator++(int); constexpr counted_iterator operator++(int)requires forward_iterator; constexpr counted_iterator& operator--()requires bidirectional_iterator; constexpr counted_iterator operator--(int)requires bidirectional_iterator; constexpr counted_iterator operator+(iter_difference_t n) constrequires random_access_iterator; friend constexpr counted_iterator operator+( iter_difference_t n, const counted_iterator& x)requires random_access_iterator; constexpr counted_iterator& operator+=(iter_difference_t n)requires random_access_iterator; constexpr counted_iterator operator-(iter_difference_t n) constrequires random_access_iterator; template<common_with I2>friend constexpr iter_difference_t operator-(const counted_iterator& x, const counted_iterator& y); friend constexpr iter_difference_t operator-(const counted_iterator& x, default_sentinel_t) noexcept; friend constexpr iter_difference_t operator-( default_sentinel_t, const counted_iterator& y) noexcept; constexpr counted_iterator& operator-=(iter_difference_t n)requires random_access_iterator; constexpr decltype(auto) operator[](iter_difference_t n) constrequires random_access_iterator; template<common_with I2>friend constexpr bool operator==(const counted_iterator& x, const counted_iterator& y); friend constexpr bool operator==(const counted_iterator& x, default_sentinel_t) noexcept; template<common_with I2>friend constexpr strong_ordering operator<=>(const counted_iterator& x, const counted_iterator& y); friend constexpr decltype(auto) iter_move(const counted_iterator& i)noexcept(noexcept(ranges::iter_move(i.current)))requires input_iterator; template<indirectly_swappable I2>friend constexpr void iter_swap(const counted_iterator& x, const counted_iterator& y)noexcept(noexcept(ranges::iter_swap(x.current, y.current))); private: I current = I(); // exposition only iter_difference_t length = 0; // exposition only}; template<input_iterator I>requires same_as<ITER_TRAITS(I), iterator_traits> // see [iterator.concepts.general]struct iterator_traits<counted_iterator> : iterator_traits {using pointer = conditional_t<contiguous_iterator, add_pointer_t<iter_reference_t>, void>; };}