std::array::front(3)

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

🌸 std::array::front(3) πŸ’…

Bestie, this is the C++ Standard Libary manual page for std::array::front(3) β€” yes, the header literally spells it β€œLibary,” and we respect the typo. It’s giving β€œfirst element access, but make it const-correct.”


✨ NAME ✨

  • std::array::front β€” std::array::front (that’s it, that’s the name section, periodt).

πŸ¦‹ Synopsis πŸ¦‹

          reference front();             (1) (since C++11)
                                             (constexpr since C++17)
          const_reference front() const; (2) (since C++11)
                                             (constexpr since C++14)

So overload (1) gives you a reference; overload (2) gives you a const_reference and is const. Both are since C++11, with constexpr since C++17 for (1) and since C++14 for (2).

It returns a reference to the first element in the container.

⚠️ Calling front on an empty container causes undefined behavior. Not a drill. Do not pass go, do not collect vibes. Check empty() first if there’s any chance it’s empty.

🧚 Parameters 🧚

(none) β€” no args, no stress, literally none.

πŸ‘‘ Return value πŸ‘‘

Reference to the first element.

⭐ Complexity ⭐

Constant. That’s the whole complexity section, bestie. No hidden scaling, no side quests.

🧠 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::array<char, 6>. The page literally says // Run this code right before the sample:

#include <array>
#include <iostream>

int main()
{
    std::array<char, 6> 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. Sign-off from the C++ Standard Libary manual: source http://cppreference.com, version/date 2024.06.10, page std::array::front(3). Slay. πŸ’…

πŸ“¦ where this page comes from