std::basic_...view::length(3)

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

πŸ’– NAME πŸ’–

okay so bestie, welcome to the std::basic_...view::length(3) page from the C++ Standard Libary manual β€” yes, that's exactly how the header spells β€œLibary”, and we are not fixing it πŸ’€. This page is about std::basic_string_view::size,std::basic_string_view::length β€” and because the original repeats it, here's the repeat: std::basic_string_view::size,std::basic_string_view::length. Periodt. πŸ’…

✨ Synopsis ✨

Both of these are constexpr, return size_type, and are const noexcept, and both have been around since C++17, no cap:

constexpr size_type size() const noexcept;    (since C++17)
constexpr size_type length() const noexcept;  (since C++17)

It returns the number of CharT elements in the view β€” i.e. std::distance(begin(), end()). That's the whole vibe. πŸ¦‹

🌸 Parameters 🌸

(none) β€” literally none, bestie. Nothing to pass, nothing to overthink. 🫢

πŸ‘‘ Return value πŸ‘‘

The number of CharT elements in the view. That's it. That's the tea. ⭐

πŸ’… Complexity πŸ’…

Constant. β€” constant time, iconic behavior, no dramatic plot twist. πŸ”₯

πŸŽ€ Example πŸŽ€

The original hits you with // Run this code, so consider this your cue to run it, bestie:

#include <iostream>
#include <string_view>

// Print a string surrounded by single quotes, its
// length and whether it is considered empty.
void check_string(std::string_view ref)
{
    std::cout << std::boolalpha
              << "'" << ref << "' has " << ref.size()
              << " character(s); emptiness: " << ref.empty() << '\n';
}

int main(int argc, char **argv)
{
    // An empty string
    check_string("");

    // Almost always not empty: argv[0]
    if (argc > 0)
        check_string(argv[0]);
}

🌈 Possible output 🌈

'' has 0 character(s); emptiness: true
'./a.out' has 7 character(s); emptiness: false

πŸ¦‹ See also πŸ¦‹

  • empty β€” checks whether the view is empty (public member function)
  • max_size β€” returns the maximum number of characters (public member function)
  • size β€” returns the number of characters
  • length β€” public member function of std::basic_string<CharT,Traits,Allocator>

mwah πŸ’‹ β€” this footer moment is brought to you by source http://cppreference.com, version/date 2024.06.10, and the page std::basic_...view::length(3). That's the cute sign-off, bestie; go forth and slay your string views. πŸ§šβ€β™€οΈπŸ’œ

πŸ“¦ where this page comes from