[counted.iterator] (original) (raw)

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_sentinelin 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.

Two values i1 and i2 of typescounted_iterator<I1>andcounted_iterator<I2>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<I>; using difference_type = iter_difference_t<I>;using iterator_concept = typename I::iterator_concept; using iterator_category = typename I::iterator_category; constexpr counted_iterator() requires default_initializable<I> = default;constexpr counted_iterator(I x, iter_difference_t<I> n);template<class I2> requires convertible_to<const I2&, I> constexpr counted_iterator(const counted_iterator<I2>& x);template<class I2> requires assignable_from<I&, const I2&> constexpr counted_iterator& operator=(const counted_iterator<I2>& x);constexpr const I& base() const & noexcept;constexpr I base() &&;constexpr iter_difference_t<I> count() const noexcept;constexpr decltype(auto) operator*();constexpr decltype(auto) operator*() const requires dereferenceable<const I>;constexpr auto operator->() const noexcept requires contiguous_iterator<I>;constexpr counted_iterator& operator++();constexpr decltype(auto) operator++(int);constexpr counted_iterator operator++(int) requires forward_iterator<I>;constexpr counted_iterator& operator--() requires bidirectional_iterator<I>;constexpr counted_iterator operator--(int) requires bidirectional_iterator<I>;constexpr counted_iterator operator+(iter_difference_t<I> n) const requires random_access_iterator<I>;friend constexpr counted_iterator operator+( iter_difference_t<I> n, const counted_iterator& x) requires random_access_iterator<I>;constexpr counted_iterator& operator+=(iter_difference_t<I> n) requires random_access_iterator<I>;constexpr counted_iterator operator-(iter_difference_t<I> n) const requires random_access_iterator<I>;template<common_with<I> I2> friend constexpr iter_difference_t<I2> operator-( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr iter_difference_t<I> operator-( const counted_iterator& x, default_sentinel_t);friend constexpr iter_difference_t<I> operator-( default_sentinel_t, const counted_iterator& y);constexpr counted_iterator& operator-=(iter_difference_t<I> n) requires random_access_iterator<I>;constexpr decltype(auto) operator[](iter_difference_t<I> n) const requires random_access_iterator<I>;template<common_with<I> I2> friend constexpr bool operator==( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr bool operator==( const counted_iterator& x, default_sentinel_t);template<common_with<I> I2> friend constexpr strong_ordering operator<=>( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr decltype(auto) iter_move(const counted_iterator& i) noexcept(noexcept(ranges::iter_move(i.current))) requires input_iterator<I>;template<indirectly_swappable<I> I2> friend constexpr void iter_swap(const counted_iterator& x, const counted_iterator<I2>& y) noexcept(noexcept(ranges::iter_swap(x.current, y.current)));private: I current = I(); iter_difference_t<I> length = 0; };template<input_iterator I> requires same_as<_ITER_TRAITS_(I), iterator_traits<I>> struct iterator_traits<counted_iterator<I>> : iterator_traits<I> { using pointer = conditional_t<contiguous_iterator<I>, add_pointer_t<iter_reference_t<I>>, void>;};}