std::deque::back(3)

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

πŸŽ€ NAME πŸŽ€

okay bestie, this is std::deque::back from the C++ Standard Libary manual, page std::deque::back(3). The NAME line is literally std::deque::back - std::deque::back β€” yes, that’s the whole name, and it’s iconic. πŸ’…

✨ Synopsis ✨

reference back();             (1)
const_reference back() const; (2)

This one returns a reference to the last element in the container. That’s the main character move. 🫢

Bestie, listen: calling back on an empty container causes undefined behavior. That is not a vibe, not a little oops β€” literally undefined behavior, periodt. πŸ’€

🌸 Parameters 🌸

(none) β€” no parameters, no extra homework. ✨

πŸ’– Return value πŸ’–

A reference to the last element. That’s it, that’s the return value. πŸŽ€

πŸ¦‹ Complexity πŸ¦‹

Constant. Lowkey the shortest flex on the whole page. ⭐

🧚 Notes 🧚

For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). No cap, that’s the exact equivalence. 🌈

🌈 Example 🌈

The example uses back to display the last element of a std::deque<char>. Run this code:

        #include <deque>
        #include <iostream>

        int main()
        {
            std::deque<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).

🌸 source/version/date sign-off: http://cppreference.com Β· 2024.06.10 Β· std::deque::back(3) β€” mwah, bye bestie! πŸ’–
πŸ“¦ where this page comes from