std::span::...pan::cbegin(3)

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

okay bestie, welcome to the C++ Standard Libary manual β€” yes, β€œLibary” is literally how it’s spelled in the header, leave it alone πŸ’…. Today’s page is the std::span::...pan::cbegin(3) moment, and it’s all about std::span::begin and std::span::cbegin. iconic behavior only ✨

πŸ’– NAME πŸ’–

std::span::begin,std::span::cbegin β€” literally std::span::begin,std::span::cbegin, no cap.

✨ Synopsis ✨

constexpr iterator begin() const noexcept;        (1) (since C++20)
constexpr const_iterator cbegin() const noexcept; (2) (since C++23)

These two cuties return an iterator to the first element of the span.

If the span is empty, the returned iterator will be equal to end().

Also: range-begin-end.svg β€” that’s the little diagram from the original, imagine it right here bestie πŸ–ΌοΈ.

🫢 Parameters 🫢

(none) β€” literally no parameters, not a single one, zero, zilch, nada. slay.

πŸŽ€ Return value πŸŽ€

An iterator to the first element. That’s the whole vibe. periodt ✨

πŸ¦‹ Complexity πŸ¦‹

Constant. Always constant. No secret extra loops, bestie. fr fr.

🌸 Example 🌸

// Run this code

#include <iostream>
#include <span>

void print(std::span<const int> array)
{
    std::cout << "array = ";
    for (auto it = array.begin(); it != array.end(); ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
}

void set_first_element(std::span<int> sp, int new_value)
{
    if (!sp.empty())
    {
        std::cout << "old *begin = " << *sp.begin() << '\n';
        *sp.begin() = new_value;
        std::cout << "new *begin = " << *sp.begin() << '\n';
    }
}

int main()
{
    int array[]{1, 3, 4, 5};
    print(array);
    set_first_element(array, 2);
    print(array);
}

Output:

array = 1 3 4 5
old *begin = 1
new *begin = 2
array = 2 3 4 5

πŸ‘‘ See also πŸ‘‘

  • end β€” returns an iterator to the end
  • cend β€” (public member function) (C++23)
  • begin
  • cbegin β€” returns an iterator to the beginning of a container or array (C++11) (function template) (C++14)

sending you kisses from http://cppreference.com πŸ’‹, dated 2024.06.10, on the std::span::...pan::cbegin(3) page β€” bye bestie! 🫢

πŸ“¦ where this page comes from