std::deque::back(3)
π NAME π
okay bestie, this is std::deque::back from the C++ Standard Libary manual, page std::deque::back(3). The NAME line is literally std::deque::back - std::deque::back β yes, thatβs the whole name, and itβs iconic. π
β¨ Synopsis β¨
reference back(); (1)
const_reference back() const; (2)
This one returns a reference to the last element in the container. Thatβs the main character move. π«Ά
Bestie, listen: calling back on an empty container causes undefined behavior. That is not a vibe, not a little oops β literally undefined behavior, periodt. π
πΈ Parameters πΈ
(none) β no parameters, no extra homework. β¨
π Return value π
A reference to the last element. Thatβs it, thatβs the return value. π
π¦ Complexity π¦
Constant. Lowkey the shortest flex on the whole page. β
π§ Notes π§
For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). No cap, thatβs the exact equivalence. π
π Example π
The example uses back to display the last element of a std::deque<char>. Run this code:
#include <deque>
#include <iostream>
int main()
{
std::deque<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'.
π See also π
frontβ access the first element (public member function).
πΈ source/version/date sign-off:http://cppreference.comΒ·2024.06.10Β·std::deque::back(3)β mwah, bye bestie! π
π¦ 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