std::basic_..._view::cend(3)

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

🌈 std::basic_..._view::cend(3) 🌈

Okay so welcome to the std::basic_..._view::cend(3) page from the C++ Standard Libary manual, bestie. This one is about std::basic_string_view::end and std::basic_string_view::cend, and it is lowkey iconic because it's all about that one-past-the-last-character energy. ✨


πŸ’– NAME πŸ’–

std::basic_string_view::end,std::basic_string_view::cend β€” std::basic_string_view::end,std::basic_string_view::cend.

Yep, the NAME line is literally doing an echo thing. Not me pointing it out, but periodt. πŸ’…


✨ Synopsis ✨

constexpr const_iterator end() const noexcept;   (since C++17)
constexpr const_iterator cend() const noexcept;  (since C++17)

Both of these return an iterator to the character following the last character of the view. That character is a placeholder, bestie, and attempting to access it results in undefined behavior. 🫢

There's also a range-begin-end.svg diagram referenced here in the original page, so respect the diagram vibes. πŸ¦‹


πŸŽ€ Parameters πŸŽ€

(none) β€” zero parameters, no arguments, not a single little input. It's giving "no thoughts, just iterator" energy. 🌸


πŸ¦‹ Return value πŸ¦‹

A const_iterator to the character following the last character. Literally that one-past-the-end spot. πŸ”₯


🌸 Complexity 🌸

Constant. That's it, bestie β€” constant. ⭐


πŸ’– Example πŸ’–

       // Run this code

        #include <iostream>
        #include <iterator>
        #include <string_view>

        int main()
        {
            constexpr std::string_view str_view("abcd");
            constexpr auto end = str_view.end();
            constexpr auto cend = str_view.cend();

            static_assert
            (
                *std::prev(end) == 'd' && 'd' == *std::prev(cend) and end ==
       cend
            );
        }

πŸ‘‘ See also πŸ‘‘

  • begin β€” returns an iterator to the beginning 🌱
  • cbegin β€” (public member function) ✨
  • end β€” returns an iterator to the end πŸ”₯
  • cend β€” (public member function of std::basic_string<CharT,Traits,Allocator>) (C++11) πŸ‘‘

Bestie, do not dereference that placeholder character. That's undefined behavior, no cap. Do not touch it. πŸ’€

Source/version/date check: http://cppreference.com, 2024.06.10, footer title std::basic_..._view::cend(3). That's the whole tea β€” bye and slay your iterators! πŸ’…βœ¨

πŸ“¦ where this page comes from