std::list::back(3)

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

πŸ’– NAME πŸ’–

std::list::back β€” std::list::back (yes, the NAME section is literally that, and honestly? mood 😭). This page is from the C++ Standard Libary β€” yep, that's how the header spells it, and we're keeping it, bestie πŸ’….


🌸 Synopsis 🌸

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

Returns a reference to the last element in the container. Simple. Iconic. Very last-element main character energy ✨.

⚠️ Calling back on an empty container causes undefined behavior. Not cute, not safe, do not do it bestie πŸ’€.

✨ Parameters ✨

(none) β€” no arguments, no parameters, nothing to pass in. She takes zero notes and serves the last element πŸŽ€.


πŸŽ€ Return value πŸŽ€

Reference to the last element. That's the whole vibe 🫢.


🧚 Complexity 🧚

Constant. Fast. Reliable. Lowkey perfect πŸ¦‹.


πŸ¦‹ Notes πŸ¦‹

For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()). No cap, that's the exact relationship πŸ”₯.


πŸ’… Example πŸ’…

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

       // Run this code

        #include <list>
        #include <iostream>

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

She ate that output, fr fr πŸ“βœ¨.


🌈 See also 🌈

  • front β€” access the first element (public member function). The other end of the list, living her own life πŸ’œ.

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::list::back(3) β€” ty for reading bestie, mwah πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from