operator-(std::counted_iterator, std::default_sentinel_t) - cppreference.com (original) (raw)

C++

Compiler support
Freestanding and hosted
Language
Standard library
Standard library headers
Named requirements
Feature test macros (C++20)
Language support library
Concepts library (C++20)
Diagnostics library
Memory management library
Metaprogramming library (C++11)
General utilities library
Containers library
Iterators library
Ranges library (C++20)
Algorithms library
Strings library
Text processing library
Numerics library
Date and time library
Input/output library
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Technical specifications
Symbols index
External libraries

[edit]

Iterator library

Iterator concepts
indirectly_readable(C++20) indirectly_writable(C++20) weakly_incrementable(C++20) incrementable(C++20) is-integer-like is-signed-integer-like(C++20)(C++20) sentinel_for(C++20) sized_sentinel_for(C++20) input_iterator(C++20) output_iterator(C++20) input_or_output_iterator(C++20) forward_iterator(C++20) bidirectional_iterator(C++20) random_access_iterator(C++20) contiguous_iterator(C++20)
Iterator primitives
input_iterator_tagoutput_iterator_tagforward_iterator_tagbidirectional_iterator_tagrandom_access_iterator_tagcontiguous_iterator_tag(C++20) iter_value_titer_difference_titer_reference_titer_const_reference_titer_rvalue_reference_titer_common_reference_t(C++20)(C++20)(C++20)(C++23)(C++20)(C++20) iterator(deprecated in C++17) iterator_traits incrementable_traits(C++20) indirectly_readable_traits(C++20)
Algorithm concepts and utilities
Indirect callable concepts
indirectly_unary_invocableindirectly_regular_unary_invocable(C++20)(C++20) indirect_unary_predicate(C++20) indirect_binary_predicate(C++20) indirect_equivalence_relation(C++20) indirect_strict_weak_order(C++20)
Common algorithm requirements
indirectly_movable(C++20) indirectly_movable_storable(C++20) indirectly_copyable(C++20) indirectly_copyable_storable(C++20) indirectly_swappable(C++20) indirectly_comparable(C++20) permutable(C++20) mergeable(C++20) sortable(C++20)
Utilities
indirect_result_t(C++20) projected(C++20) projected_value_t(C++26)
Iterator adaptors
reverse_iterator make_reverse_iterator(C++14) move_iterator(C++11) make_move_iterator(C++11) default_sentinel_tdefault_sentinel(C++20)(C++20) unreachable_sentinel_tunreachable_sentinel(C++20)(C++20) front_insert_iterator back_insert_iterator inserter insert_iterator front_inserter back_inserter move_sentinel(C++20) common_iterator(C++20) counted_iterator(C++20) basic_const_iterator(C++23) const_iterator(C++23) const_sentinel(C++23) make_const_iterator(C++23) make_const_sentinel(C++23)
Stream iterators istream_iterator ostream_iterator istreambuf_iterator ostreambuf_iterator Iterator customization points ranges::iter_move(C++20) ranges::iter_swap(C++20)
Iterator operations advance distance prev(C++11) next(C++11) ranges::advance(C++20) ranges::distance(C++20) ranges::prev(C++20) ranges::next(C++20) Range access begincbegin(C++11)(C++14) rbegincrbegin(C++14)(C++14) endcend(C++11)(C++14) rendcrend(C++14)(C++14) sizessize(C++17)(C++20) empty(C++17) data(C++17)

[edit]

std::counted_iterator

Member functions
counted_iterator::counted_iterator
counted_iterator::operator=
counted_iterator::base
counted_iterator::count
counted_iterator::operator*counted_iterator::operator->
counted_iterator::operator[]
counted_iterator::operator++counted_iterator::operator++(int)counted_iterator::operator+counted_iterator::operator+=counted_iterator::operator--counted_iterator::operator--(int)counted_iterator::operator-counted_iterator::operator-=
Non-member functions
operator==operator<=>(C++20)(C++20)
operator==(default_sentinel_t)(C++20)
operator+(C++20)
operator-(C++20)
operator-(default_sentinel_t)(C++20)
iter_move(C++20)
iter_swap(C++20)
Helper classes
iterator_traitsstd::counted\_iterator(C++20)

[edit]

friend constexpr std::iter_difference_t<I> operator-( const counted_iterator& x, std::default_sentinel_t ); (1) (since C++20)
friend constexpr std::iter_difference_t<I> operator-( std::default_sentinel_t, const counted_iterator& y ); (2) (since C++20)
  1. Returns the negative distance to the end.

  2. Returns the positive distance to the end.

This function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::counted_iterator is an associated class of the arguments.

Contents

[edit] Parameters

x, y - iterator adaptors to compute the difference of

[edit] Return value

  1. -x.count()

  2. y.count()

[edit] Example

Run this code

#include #include   int main() { constexpr static auto v = {1, 2, 3, 4}; constexpr std::counted_iterator<std::initializer_list::iterator> it{v.begin(), 3}; constexpr auto d1 = it - std::default_sentinel; static_assert(d1 == -3); // (1) constexpr auto d2 = std::default_sentinel - it; static_assert(d2 == +3); // (2) }

[edit] See also

operator++operator++(int)operator+=operator+operator--operator--(int)operator-=operator- advances or decrements the counted_iterator (public member function) [edit]
operator+(C++20) advances the iterator (function template) [edit]
operator-(C++20) computes the distance between two iterator adaptors (function template) [edit]
default_sentinel_t(C++20) default sentinel for use with iterators that know the bound of their range (class) [edit]