std::forward_list::empty(3)

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

πŸ’– std::forward_list::empty(3) πŸ’–

okay so welcome to std::forward_list::empty(3) from the C++ Standard Libary β€” yes bestie, that’s the manual name and we’re rolling with it. This is literally the vibe check for your std::forward_list: is it empty or not? Main character energy for a tiny function, honestly. πŸ’…


πŸŽ€ NAME πŸŽ€

std::forward_list::empty β€” std::forward_list::empty

yes, the name is the name and we love a self-referential icon. no notes. πŸ’–


🌸 Synopsis 🌸

bool empty() const noexcept;                (since C++11)
                                                      (until C++20)
[[nodiscard]] bool empty() const noexcept;  (since C++20)

She checks if the container has no elements, i.e. whether begin() == end(). That’s the whole assignment. 🫢


πŸ’œ Parameters πŸ’œ

(none)

literally zero parameters. not a single one. she takes no arguments and still serves. ✨


🌟 Return value 🌟

true if the container is empty, false otherwise.

that’s it. that’s the answer. cute, concise, correct. πŸŽ€


πŸ”₯ Complexity πŸ”₯

Constant.

no dramatic runtime arc, no surprise side quest. constant energy. πŸ’€


πŸ¦‹ Example πŸ¦‹

The following code uses empty to check if a std::forward_list<int> contains any elements:

// Run this code

        #include <forward_list>
        #include <iostream>

        int main()
        {
            std::forward_list<int> numbers;
            std::cout << std::boolalpha;
            std::cout << "Initially, numbers.empty(): " << numbers.empty() <<
       '\n';

            numbers.push_front(42);
            numbers.push_front(13317);
            std::cout << "After adding elements, numbers.empty(): " <<
       numbers.empty() << '\n';
        }

✨ Output ✨

Initially, numbers.empty(): true
After adding elements, numbers.empty(): false

look at her go. empty at first, then not empty after push_front(42) and push_front(13317). iconic behavior. 🌈


πŸ‘‘ See also πŸ‘‘

  • distance β€” returns the distance between two iterators (function template)

catch you later, bestie β€” this page came from http://cppreference.com, dated 2024.06.10, and it’s signing off with maximum sparkle. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from