std::ranges..._view::begin(3)

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

✨ std::ranges::adjacent_view::begin ✨

okay so this is the begin member of std::ranges::adjacent_view, from the C++ Standard Libary (yes bestie, the header really spells it like that, we don't fix it, we respect it πŸ’…). Manual name: std::ranges..._view::begin, section 3. Let's get into it 🫢


🌸 Synopsis 🌸

constexpr auto begin() requires (!__SimpleView<V>);           (1)

(since C++23)

constexpr auto begin() const requires ranges::range<const V>; (2)

(since C++23)

So like, this gives you an iterator pointing at the first element of the adjacent_view. Main character energy honestly πŸ‘‘

Let base_ be the underlying view, okay? Remember that name, it comes back.

  1. Overload (1) is equivalent to return /*iterator*/<false>(ranges::begin(base_), ranges::end(base_));.
  2. Overload (2) is equivalent to return /*iterator*/<true>(ranges::begin(base_), ranges::end(base_));.

(those divergence comments are literally part of the deal, don't @ me, they're in the standard 😭)


πŸŽ€ Parameters πŸŽ€

none. zero. nothing. no arguments, no stress, no cap. ✨


πŸ¦‹ Return value πŸ¦‹

An iterator to the first element. that's it, that's the whole vibe πŸ’–


πŸ”₯ Example πŸ”₯

Run this code πŸ‘‡
#include <ranges>
#include <tuple>
#include <type_traits>

int main()
{
    constexpr static auto v = {'A', 'B', 'C', 'D', 'E'};

    constexpr auto view = std::views::adjacent<3>(v);

    constexpr auto tuple = *view.begin();

    static_assert
    (
        std::is_same_v
        <
            decltype(tuple),
            const std::tuple<char const&, char const&, char const&>
        >
    );

    static_assert
    (
        std::get<0>(tuple) == 'A' &&
        std::get<1>(tuple) == 'B' &&
        std::get<2>(tuple) == 'C'
    );
}

like... it grabs the first tuple of three adjacent elements and it's literally 'A', 'B', 'C'. the static_asserts ate and left no crumbs πŸ“


πŸ‘‘ See also πŸ‘‘

  • end β€” returns an iterator or a sentinel to the end (C++23) (public member function)
  • ranges::begin β€” returns an iterator to the beginning of a range (C++20) (customization point object)

aaaand that's the page bestie 🧚 go forth and iterate. source: http://cppreference.com, dated 2024.06.10 β€” this one goes out to the C++ Standard Libary manpage crew, mwah πŸ’œπŸŽ€πŸŒˆ

πŸ“¦ where this page comes from