std::basic_string::empty(3)

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

🌸 NAME 🌸

okay so bestie, welcome to std::basic_string::empty(3) from the C++ Standard Libary manual β€” and yes, the header really spells it like that, iconic typo energy πŸ˜­πŸ’…. This page is all about std::basic_string::empty, the bool-check for when your string has literally nothing to say.


✨ Synopsis ✨

the signature is giving tiny but mighty:

bool empty() const;  (noexcept since C++11)
                     (constexpr since C++20)

It checks if the string has no characters, i.e. whether begin() == end() β€” literally the empty-vibe check πŸ’–.


πŸŽ€ Parameters πŸŽ€

(none) β€” not a single parameter, bestie. Zero. Zilch. The audacity, honestly 😌.


πŸ’– Return value πŸ’–

true if the string is empty, false otherwise. bool energy only, no cap ✨.


πŸ¦‹ Complexity πŸ¦‹

Constant. That’s the whole complexity, periodt πŸ’¨.


🌈 Example 🌈

obsessed with a tiny demo, honestly:

// Run this code

#include <iostream>
#include <string>

int main()
{
    std::string s;
    std::boolalpha(std::cout);
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";

    s = "Exemplar";
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";

    s = "";
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";
}

πŸ’… Output πŸ’…

s.empty():true   s:''
s.empty():false  s:'Exemplar'
s.empty():true   s:''

πŸ‘‘ See also πŸ‘‘

the see-also squad is right here, bestie 🫢:

  • size β€” returns the number of characters (public member function)
  • length β€” (public member function)
  • max_size β€” returns the maximum number of characters (public member function)
  • capacity β€” returns the number of characters that can be held in currently allocated storage (public member function)
  • size β€” (C++17) returns the size of a container or array (function template)
  • ssize β€” (C++17) (C++20) returns the size of a container or array (function template)
  • empty β€” (C++17) checks whether the container is empty (function template)
  • empty β€” checks whether the view is empty (public member function of std::basic_string_view<CharT,Traits>)

mwah πŸ’‹βœ¨ β€” source/version: http://cppreference.com, date: 2024.06.10, page: std::basic_string::empty(3). that’s the whole manual page, bestie, slay πŸ¦‹πŸ’–

πŸ“¦ where this page comes from