std::vector::back(3)
okay so bestie, welcome to std::vector::back(3) β this page lives in the C++ Standard Libary manual, and we are about to get that last-element situation handled. π
β¨
NAME π
std::vector::back β std::vector::back. that's the name, and honestly the NAME section is giving minimal effort, but we respect it. π«Ά
Synopsis π
Two overloads, and both are constexpr since C++20:
reference back(); (1) (constexpr since C++20)
const_reference back() const; (2) (constexpr since C++20)
Returns a reference to the last element in the container. slay.
β οΈ Calling back on an empty container causes undefined behavior. not a cute vibe, not a drill, bestie ππ
Parameters π«Ά
(none) β literally none. no parameters, no extra drama.
Return value π
Reference to the last element. periodt.
Complexity β‘
Constant. iconic behavior, no cap.
Notes π§
For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). iykyk β¨
Example πΈ
This lil code uses back to display the last element of a std::vector<char>:
// Run this code
#include <vector>
#include <iostream>
int main()
{
std::vector<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).
okay bye bestie π«Άπ β source: http://cppreference.com, version/date: 2024.06.10, page: std::vector::back(3). 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