std::span::...:span::cend(3)

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

🌸 NAME 🌸

std::span::end,std::span::cend β€” std::span::end,std::span::cend

Hii bestie πŸ’– welcome to the std::span::...:span::cend(3) page from the C++ Standard Libary (yes, β€œLibary” is literally how it’s printed, iykyk 😭). Today’s main characters are end and cend, and they are lowkey iconic.

✨ Synopsis ✨

constexpr iterator end() const noexcept;        (1) (since C++20)
constexpr const_iterator cend() const noexcept; (2) (since C++23)

Okay so bestie, this returns an iterator to the element following the last element of the span.

That element is literally a placeholder β€” attempting to access it results in undefined behavior. 🚨 Not a drill, do not dereference the placeholder. The original page also has a diagram filename, range-begin-end.svg, doing its little visual moment.

πŸ’– Parameters πŸ’–

  • (none) β€” none at all, periodt.

πŸ¦‹ Return value πŸ¦‹

Iterator to the element following the last element. That’s the whole vibe. Slay. ✨

πŸŽ€ Complexity πŸŽ€

Constant. ⚑

πŸ”₯ 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 🌈

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

Bye bestie 🫢 that’s the whole page β€” page std::span::...:span::cend(3), source http://cppreference.com, version/date 2024.06.10. Mwah πŸ’‹

πŸ“¦ where this page comes from