std::basic_...view::length(3)
π 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 characterslengthβ public member function ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms