std::vector::front(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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