std::list::back(3)
π NAME π
std::list::back β std::list::back (yes, the NAME section is literally that, and honestly? mood π). This page is from the C++ Standard Libary β yep, that's how the header spells it, and we're keeping it, bestie π
.
πΈ Synopsis πΈ
reference back(); (1)
const_reference back() const; (2)
Returns a reference to the last element in the container. Simple. Iconic. Very last-element main character energy β¨.
β οΈ Calling back on an empty container causes undefined behavior. Not cute, not safe, do not do it bestie π.
β¨ Parameters β¨
(none) β no arguments, no parameters, nothing to pass in. She takes zero notes and serves the last element π.
π Return value π
Reference to the last element. That's the whole vibe π«Ά.
π§ Complexity π§
Constant. Fast. Reliable. Lowkey perfect π¦.
π¦ Notes π¦
For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). No cap, that's the exact relationship π₯.
π Example π
The following code uses back to display the last element of a std::list<char>:
// Run this code
#include <list>
#include <iostream>
int main()
{
std::list<char> letters{'a', 'b', 'c', 'd', 'e', 'f'};
if (!letters.empty())
std::cout << "The last character is '" << letters.back() <<
"'.\n";
}
Output:
The last character is 'f'.
She ate that output, fr fr πβ¨.
π See also π
frontβ access the first element (public member function). The other end of the list, living her own life π.
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::list::back(3) β ty for reading bestie, 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