std::counted_iterator
is an iterator adaptor which behaves exactly like the underlying iterator, except that it keeps track of the distance to the end of its range. This iterator is equal to std::default_sentinel if and only if its count reaches zero.
iterator_type
I
value_type
I
models indirectly_readable
; otherwise, not defined difference_type
std::iter_difference_t<I> iterator_concept
iterator_category
current
(private) the underlying iterator which base()
accesses
length
(private) the distance between the underlying iterator and the end of its range
#include <algorithm> #include <iostream> #include <iterator> #include <string> #include <vector> using std::operator""s; void print(auto const remark, auto const& v) { const auto size = std::ssize(v); std::cout << remark << '[' << size << "] { "; for (auto it = std::counted_iterator{std::cbegin(v), size}; it != std::default_sentinel; ++it) std::cout << *it << (it.count() > 1 ? ", " : " "); std::cout << "}\n"; } int main() { const auto src = {"Arcturus"s, "Betelgeuse"s, "Canopus"s, "Deneb"s, "Elnath"s}; print("src", src); std::vector<decltype(src)::value_type> dst; std::ranges::copy(std::counted_iterator{src.begin(), 3}, std::default_sentinel, std::back_inserter(dst)); print("dst", dst); }
Output:
src[5] { Arcturus, Betelgeuse, Canopus, Deneb, Elnath } dst[3] { Arcturus, Betelgeuse, Canopus }[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
[edit] See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4