std::array::back(3)

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

okay bestie, you’re reading the std::array::back(3) page from the C++ Standard Libary manual, and it’s literally all about peeking at the last element. slay πŸ’…βœ¨

πŸŽ€ NAME

std::array::back β€” yes, it’s giving std::array::back, no cap 🫢

✨ Synopsis ✨

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

It returns a reference to the last element in the container. main character energy πŸ‘‘

Warning bestie: calling back on an empty container causes undefined behavior 😭 fr fr, don’t do it.

🌸 Parameters

none, zero, zilch, nada 🫢

πŸ’– Return value

A reference to the last element. periodt.

πŸ¦‹ Complexity

Constant. always. πŸ’€βœ¨

🧚 Notes

For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). iykyk 🌸

⭐ Example

The following code uses back to display the last element of a std::array<char>:

// Run this code

#include <array>
#include <iostream>

int main()
{
    std::array<char, 6> 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'.

ate, truly πŸ“βœ¨

πŸ’œ See also

  • front β€” access the first element (public member function) πŸ‘‘πŸ¦‹

byeee πŸ’– this iconic page came from http://cppreference.com, dated 2024.06.10, and it’s the std::array::back(3) page. mwah πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from