std::basic_string_view(3)
π std::basic_string_view π
okay bestie, this is the std::basic_string_view(3) page from the C++ Standard Library section β yes, the header really spells it C++ Standard Libary, and honestly π. This is part 1, so buckle up; the facts are all here, the vibe is just, like, way more fun now π
.
π NAME π
std::basic_string_view β std::basic_string_view (yes bestie, the NAME section is literally the same string twice; we love a minimal icon π).
β¨ Synopsis β¨
okay so check it: this is defined in header <string_view>, and the template situation is:
Defined in header <string_view>
template<
class CharT, (since C++17)
class Traits = std::char_traits<CharT>
> class basic_string_view;
The class template basic_string_view describes an object that can refer to a constant contiguous sequence of CharT with the first element of the sequence at position zero. Slay, but make it read-only β¨.
For a basic_string_view str, pointers, iterators, and references to elements of str are invalidated when an operation invalidates a pointer in the range [str.data(), str.data() + str.size()). That range is literally the whole vibe check, bestie.
Every specialization of std::basic_string_view is a TriviallyCopyable (since C++23) type. A typical implementation holds only two members: a pointer to constant CharT and a size. Lowkey elegant, no cap π.
Several typedefs for common character types are provided:
Defined in header <string_view>
std::string_view(C++17) βstd::basic_string_view<char>std::wstring_view(C++17) βstd::basic_string_view<wchar_t>std::u8string_view(C++20) βstd::basic_string_view<char8_t>std::u16string_view(C++17) βstd::basic_string_view<char16_t>std::u32string_view(C++17) βstd::basic_string_view<char32_t>
π Template parameters π
CharTβ character type.Traitsβ CharTraits class specifying the operations on the character type. Like forstd::basic_string,Traits::char_typemust name the same type asCharTor the program is ill-formed.
π¦ Nested types π¦
traits_typeβTraitsvalue_typeβCharTpointerβCharT*const_pointerβconst CharT*referenceβCharT&const_referenceβconst CharT&const_iteratorβ implementation-defined constantLegacyRandomAccessIterator, andLegacyContiguousIterator(until C++20);ConstexprIterator, andcontiguous_iterator(since C++20), whosevalue_typeisCharTiteratorβconst_iteratorconst_reverse_iteratorβstd::reverse_iterator<const_iterator>reverse_iteratorβconst_reverse_iteratorsize_typeβstd::size_tdifference_typeβstd::ptrdiff_t
Note:iteratorandconst_iteratorare the same type because string views are views into constant character sequences. Iconic behavior, honestly π«Ά.
All requirements on the iterator types of a Container applies to the iterator and const_iterator types of basic_string_view as well. fr fr.
π Member functions π
Here's the whole menu, grouped exactly like the page groups it, bestie.
Constructors and assignment
constructorβ constructs abasic_string_view(public member function)operator=β assigns a view (public member function)
Iterators
beginβ returns an iterator to the beginning (public member function)cbeginβ (public member function)endβ returns an iterator to the end (public member function)cendβ (public member function)rbeginβ returns a reverse iterator to the beginning (public member function)crbeginβ (public member function)rendβ returns a reverse iterator to the end (public member function)crendβ (public member function)
Element access
operator[]β accesses the specified character (public member function)atβ accesses the specified character with bounds checking (public member function)frontβ accesses the first character (public member function)backβ accesses the last character (public member function)dataβ returns a pointer to the first character of a view (public member function)
Capacity
sizeβ returns the number of characters (public member function)lengthβ (public member function), listed right undersize's count descriptionmax_sizeβ returns the maximum number of characters (public member function)emptyβ checks whether the view is empty (public member function)
Modifiers
remove_prefixβ shrinks the view by moving its start forward (public member function)remove_suffixβ shrinks the view by moving its end backward (public member function)swapβ swaps the contents (public member function)
Operations
copyβ copies characters (public member function)substrβ returns a substring (public member function)compareβ compares two views (public member function)starts_with(C++20) β checks if the string view starts with the given prefix (public member function)ends_with(C++20) β checks if the string view ends with the given suffix (public member function)contains(C++23) β checks if the string view contains the given substring or character (public member function)findβ find characters in the view (public member function)rfindβ find the last occurrence of a substring (public member function)find_first_ofβ find first occurrence of characters (public member function)find_last_ofβ find last occurrence of characters (public member function)find_first_not_ofβ find first absence of characters (public member function)find_last_not_ofβ find last absence of characters (public member function)
π Constants π
nposβ special value. The exact meaning depends on the context[static](public static member constant)
βοΈ Non-member functions βοΈ
The comparison crew is all here:
operator==operator!=operator<operator>operator<=operator>=operator<=>β lexicographically compares two string views
The exact status-tag stack on that block, in order, is giving chaotic table energy π:
(C++17) (function template)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(removed in C++20)
(C++20)
πΊ Input/output πΊ
operator<<(C++17) β performs stream output on string views (function template)
π Literals π
Defined in inline namespace std::literals::string_view_literals.
operator""sv(C++17) β creates a string view of a character array literal (function)
π§ Helper classes π§
Hash support for string views is provided by these class template specializations:
std::hash<std::string_view>β (C++17)std::hash<std::wstring_view>β (C++17)std::hash<std::u8string_view>β (C++20)std::hash<std::u16string_view>β (C++17)std::hash<std::u32string_view>β (C++17)
All of that is hash support for string views (C++17) (class template specialization). The version tags sit in this order in the original:(C++17)(C++20)(C++17)(C++17)β yes bestie, the formatting is a whole journey π.
π¦ Helper templates π¦
First one, no cap:
template< class CharT, class Traits >
inline constexpr bool
(since C++20)
ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> =
true;
This specialization of ranges::enable_borrowed_range makes basic_string_view satisfy borrowed_range. Periodt.
Second one:
template< class CharT, class Traits >
inline constexpr bool
(since C++20)
ranges::enable_view<std::basic_string_view<CharT, Traits>> =
true;
This specialization of ranges::enable_view makes basic_string_view satisfy view. Obsessed with the consistency π.
π Deduction guides (since C++20) π
The page announces Deduction guides (since C++20) here; no extra guide lines are shown in the supplied text before Notes. That's literally the whole section in this part, bestie.
π₯ Notes π₯
It is the programmer's responsibility to ensure that std::string_view does not outlive the pointed-to character array:
std::string_view good{"a string literal"};
// "Good" case: `good` points to a static array.
// String literals reside in persistent data storage.
std::string_view bad{"a temporary string"s};
// "Bad" case: `bad` holds a dangling pointer since the std::string temporary,
// created by std::operator""s, will be destroyed at the end of the statement.
Specializations of std::basic_string_view are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.
Feature-test macro table, bestie:
__cpp_lib_string_viewβ201606L(C++17) βstd::string_view__cpp_lib_string_viewβ201803L(C++20) βConstexprIterator__cpp_lib_string_containsβ202011L(C++23) βcontains
π Example π
Run this code, bestie:
// Run this code
#include <iostream>
#include <string_view>
int main()
{
constexpr std::string_view unicode[]{"βββ", "βββ", "βββ", "βββ"};
for (int y{}, p{}; y != 6; ++y, p = ((p + 1) % 4))
{
for (int x{}; x != 16; ++x)
std::cout << unicode[p];
std::cout << '\n';
}
}
πΊ Output
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββ
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3203β Applied to:C++17. Behavior as published: only pointers, iterators, and references returned from the member functions ofbasic_string_viewmight be invalidated. Correct behavior: all pointers, iterators, and references to elements ofbasic_string_viewmay be invalidated.
β¨ SEE ALSO β¨
okay bestie, you made it to the little link garden at the end πΈ these are the related pages, and yes theyβre all class templates β we love a consistent vibe π
basic_stringβ stores and manipulates sequences of characters π (class template)spanβ a non-owning view over a contiguous sequence of objects π (C++20) (class template)initializer_listβ creates a temporary array in list-initialization and then (C++11) references it π (class template)
thatβs a wrap on std::basic_string_view(3), bestie π source/version http://cppreference.com, date 2024.06.10, mwah π«Ά
π¦ 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