std::counte...ator::count(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::counte...ator::count(3) πŸ’–

okay bestie, welcome to the <std::counte...ator::count(3)> page from the C++ Standard Libary manual (yes, that spelling, no cap πŸ’…). This is the std::counted_iterator::count moment, and it’s giving very specific, very useful energy ✨.

πŸ’– NAME πŸ’–

std::counted_iterator::count - std::counted_iterator::count 🫢

fr fr, the NAME line is literally echoing itself, and honestly? iconic.

✨ Synopsis ✨

The signature is here, and it’s been serving since C++20:

constexpr std::iter_difference_t<I> count() const noexcept;  (since
C++20)

So what does it do? It returns the underlying length that is the distance to the end. That’s the whole vibe check πŸ’–.

πŸŽ€ Parameters πŸŽ€

(none) β€” zero, zilch, not a single parameter, bestie πŸ™ˆ.

πŸ¦‹ Return value πŸ¦‹

The underlying length. That’s it. That’s the return value, periodt πŸ’….

🌸 Example 🌸

// Run this code β€” yes bestie, run it, it’s literally right there ✨

        #include <cassert>
        #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);
            auto i2{i1};
            for (; std::default_sentinel != i2; ++i2)
                std::cout << "*i2: " << *i2 << ", count(): " << i2.count() <<
       '\n';
            assert(i2.count() == 0);
        }

πŸ”₯ Output πŸ”₯

        *i2: 2, count(): 3
        *i2: 3, count(): 2
        *i2: 4, count(): 1

look at that little count() countdown, absolutely ate πŸ”₯

πŸ‘‘ See also πŸ‘‘

  • base β€” accesses the underlying iterator πŸ” (C++20) (public member function)

mwah πŸ’‹ this iconic little page comes from http://cppreference.com, dated 2024.06.10, and its footer name is std::counte...ator::count(3). byeeee πŸ«ΆπŸ’–

πŸ“¦ where this page comes from