std::counte...ted_iterator(3)
π NAME π
Okay so this page is std::counte...ted_iterator(3) from the C++ Standard Libary, and the NAME section says it twice: std::counted_iterator::counted_iterator β yes, bestie, it's the constructor for the counted iterator adaptor. The original NAME line is literally std::counted_iterator::counted_iterator - std::counted_iterator::counted_iterator, so we're keeping that double-name energy. π
β¨
β¨ Synopsis β¨
Here are the three constructor forms, all marked (since C++20) in the original:
(1)(since C++20) β default constructor vibes:
constexpr counted_iterator() requires std::default_initializable<I> = default;
(2)(since C++20) β iterator plus length:
constexpr counted_iterator( I x, std::iter_difference_t<I> n );
(3)(since C++20) β converting constructor:
template< class I2 >
requires std::convertible_to<const I2&, I>
constexpr counted_iterator( const counted_iterator<I2>& other );
Constructs a new iterator adaptor. That's the whole job, and it's iconic. π«Ά
(1)Default constructor. Value-initializes the underlying iterator and initialized the underlying length with0. Operations on the resulting iterator have defined behavior if and only if the corresponding operations on a value-initializedIalso have defined behavior. πΈ(2)The underlying iterator is initialized withstd::move(x)and the underlying length is initialized withn. The behavior is undefined ifnis negative. π¨ Like, do not pass a negativen, fr fr.(3)The underlying iterator and length are initialized with those ofother. Slay, conversion. π«
π Parameters π
xβ iterator to adaptnβ distance to the endotherβ iterator adaptor to convert
πΈ Example πΈ
The page has a // Run this code line before the block, so go run it bestie:
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
int main()
{
static constexpr auto pi = {3, 1, 4, 1, 5, 9, 2};
// (1) default constructor:
constexpr
std::counted_iterator<std::initializer_list<int>::iterator> i1{};
static_assert(i1 == std::default_sentinel);
static_assert(i1.count() == 0);
// (2) initializes the iterator and length respectively:
constexpr
std::counted_iterator<std::initializer_list<int>::iterator> i2{
pi.begin(), pi.size() - 2
};
static_assert(i2.count() == 5);
static_assert(*i2 == 3 && i2[1] == 1);
// (3) converting-constructor:
std::counted_iterator<std::initializer_list<const int>::iterator>
i3{i2};
std::ranges::copy(i3, std::default_sentinel,
std::ostream_iterator<const int>{std::cout, " "});
}
π¦ Output π¦
3 1 4 1 5
π See also π
operator=β assigns another iterator adaptor;(C++20)(public member function)π
That's the whole page, bestie π. Source/version vibes: http://cppreference.com, dated 2024.06.10. Go construct something iconic π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms