std::list::...:list::cend(3)
π 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 beginningcbeginβ (public member function)(C++11)endcendβ 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms