std::counted_iterator<I>::count

From cppreference.com
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Utilities
(C++20)
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
(C++11)
(C++11)
Range access
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 
 
constexpr std::iter_difference_t<I> count() const noexcept;
(since C++20)

Returns the underlying length that is the distance to the end.

Parameters

(none)

Return value

the underlying length.

Example

#include <iostream>
#include <iterator>
 
int main()
{
    constexpr static auto il = { 1, 2, 3, 4, 5 };
    constexpr std::counted_iterator i1{il.begin() + 1, 3};
    static_assert(i1.count() == 3);
    for (auto i2{i1}; std::default_sentinel != i2; ++i2)
        std::cout << *i2 << ' ';
    std::cout << '\n';
}

Output:

2 3 4

See also

(C++20)
accesses the underlying iterator
(public member function)