std::vector::front(3)
std::vector::front(3) β C++ Standard Libary π
okay so bestie, welcome to the std::vector::front page from the C++ Standard Libary (yes, the header spells it βLibaryβ β weβre keeping the typo, itβs iconic). This one is short, sweet, and lowkey essential: it hands you the first element of your vector. Letβs get into it. π¦β¨
NAME π
std::vector::front - std::vector::front
Thatβs the NAME section, and it said the same thing twice. We respect it. π
Synopsis π¦
reference front(); (1) (constexpr since C++20)
const_reference front() const; (2) (constexpr since C++20)
Returns a reference to the first element in the container.
β οΈ Calling front on an empty container causes undefined behavior. Like, actual undefined behavior. Not a cute look. Check that container first, bestie. ππ
Parameters π
(none)
Literally no parameters. Zero. Nada. The parameter list is empty and itβs serving minimalist main character energy. π
Return value π
Reference to the first element.
Thatβs it. Thatβs the return value. First element, reference, slay. β¨
Complexity β
Constant.
Constant. No cap. β
Notes πΈ
For a container c, the expression c.front() is equivalent to *c.begin().
iykyk. π«Ά
Example π₯
The following code uses front to display the first element of a std::vector<char>:
(The original page also had // Run this code β thatβs the official example vibe. Hereβs the code:)
#include <vector>
#include <iostream>
int main()
{
std::vector<char> letters{'a', 'b', 'c', 'd', 'e', 'f'};
if (!letters.empty())
std::cout << "The first character is '" << letters.front() <<
"'.\n";
}
Output:
The first character is 'a'.
See also π
backβ access the last element (public member function)
okay thatβs the whole page, bestie. It came from http://cppreference.com, version/date 2024.06.10, and itβs the std::vector::front(3) page. Go get that first element β but check empty() first, fr fr. πβ¨π
π¦ 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