std::list::...:list::cend(3)

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

πŸ’œ std::list::end, std::list::cend πŸ’œ

okay bestie, we're doing a cppreference page today β€” manual std::list::...:list::cend(3) from the C++ Standard Library, and it's honestly so much cuter than it sounds. two little member functions, one job: point at the fake element after the end. slay.


✨ NAME ✨

std::list::end, std::list::cend β€” literally just std::list::end and std::list::cend, no cap 🫢


🌸 SYNOPSIS 🌸

iterator end();                       (1) (noexcept since C++11)
const_iterator end() const;           (2) (noexcept since C++11)
const_iterator cend() const noexcept; (3) (since C++11)

So there's three flavors, and honestly the way that overload (1) and (2) got noexcept in C++11? iconic. overload (3) was born in C++11 already noexcept, main character energy from day one πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Returns an iterator to the element following the last element of the list. πŸŽ€

⚠️ bestie listen to me. this element is a placeholder. it is not a real element. attempting to access it results in undefined behavior. do NOT dereference it. i'm serious. it's giving "do not perceive" energy. πŸ’€

range-:begin-:end β€” yeah that little diagram range-begin-end.svg lives here, it's the classic half-open range picture, you know the one.


πŸŽ€ PARAMETERS πŸŽ€

(none) β€” nothing to pass, she takes zero notes, zero arguments. obsessed. ✨


🌈 RETURN VALUE 🌈

An iterator to the element following the last element. that's it. that's the whole vibe.


⭐ COMPLEXITY ⭐

Constant. O(1) babe, it's instant, no scrolling required πŸ”₯


πŸ’– NOTES πŸ’–

libc++ backports cend() all the way to C++98 mode. so if you're living in the past, libc++ has you covered. periodt. πŸ‘‘


πŸ“ EXAMPLE πŸ“

// Run this code

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <list>

int main()
{
    std::list<int> nums{1, 2, 4, 8, 16};
    std::list<std::string> fruits{"orange", "apple", "raspberry"};
    std::list<char> empty;

    // Print list.
    std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';

    // Sums all integers in the list nums (if any), printing only the result.
    std::cout << "Sum of nums: "
              << std::accumulate(nums.begin(), nums.end(), 0) << '\n';

    // Prints the first fruit in the list fruits, checking if there is any.
    if (!fruits.empty())
        std::cout << "First fruit: " << *fruits.begin() << '\n';

    if (empty.begin() == empty.end())
        std::cout << "list 'empty' is indeed empty.\n";
}

🌸 Output:

1 2 4 8 16
Sum of nums: 31
First fruit: orange
list 'empty' is indeed empty.

that last check with empty.begin() == empty.end()? the way it just knows the list is empty. also that std::accumulate adding up to 31? ate and left no crumbs 🧚


πŸ‘‘ SEE ALSO πŸ‘‘

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

iykyk, the whole begin/end family is the friend group and you gotta know all of them πŸ’œ


okay that's the page!! source is cppreference, dated 2024.06.10, and the footer manual line was std::list::...:list::cend(3) β€” go dereference responsibly, or actually, don't dereference the end iterator at all πŸ’€πŸ’–βœ¨

πŸ“¦ where this page comes from