std::list::...list::crend(3)
okay bestie, this is the std::list::...list::crend(3) page from the C++ Standard Libary manual β yes, that spelling is the manualβs, and weβre keeping it π
.
β¨ NAME β¨
std::list::rend,std::list::crend - std::list::rend,std::list::crend
these are the reverse-end besties for std::list β the iconique duo rend + crend π.
πΈ Synopsis πΈ
hereβs the signature lineup, no cap:
reverse_iterator rend(); (1) (noexcept since C++11)
const_reverse_iterator rend() const; (2) (noexcept since C++11)
const_reverse_iterator crend() const noexcept; (3) (since C++11)
(1)βreverse_iterator rend();, and itβsnoexceptsince C++11 β¨(2)βconst_reverse_iterator rend() const;, alsonoexceptsince C++11 π(3)βconst_reverse_iterator crend() const noexcept;, since C++11 π¦
so whatβs the vibe? rend() and crend() return a reverse iterator to the element following the last element of the reversed list. It corresponds to the element preceding the first element of the non-reversed list. This element acts as a placeholder β attempting to access it results in undefined behavior ππ, so do not play with it.
the original also has a diagram file called range-rbegin-rend.svg if you want the visual π¦.
π Parameters π
(none) β literally no parameters, bestie. nothing to pass, nothing to stress about π«Ά.
π Return value π
Reverse iterator to the element following the last element. thatβs the whole sentence, periodt π .
β Complexity β
Constant. lowkey effortless, highkey constant β¨.
π§ Notes π§
libc++ backports crend() to C++98 mode. thatβs the note, iykyk π§.
π 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.rbegin(), nums.rend(), [](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.rbegin(), nums.rend(), 0) <<
'\n';
// Prints the first fruit in the list fruits, checking if there is
any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.rbegin() << '\n';
if (empty.rbegin() == empty.rend())
std::cout << "list 'empty' is indeed empty.\n";
}
π Output: π
16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
list 'empty' is indeed empty.
π See also π
rbeginβ returns a reverse iterator to the beginningcrbeginβ (public member function) (C++11)rendβ returns a reverse end iterator for a container or arraycrendβ (function template) (C++14)
π and thatβs the std::list::...list::crend(3) page, bestie β source/version: http://cppreference.com, dated 2024.06.10. mwah π
β¨
π¦ 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