template<classCharT,classTraits=std::char_traits<CharT>>classbasic_string_view;(since C++17)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.
For a basic_string_viewstr, 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()).
Every specialization of std::basic_string_view is a
type.
(since C++23)Several typedefs for common character types are provided:
Defined in header
Type Definition
(C++17)std::basic_string_view<char>
(C++17)std::basic_string_view<wchar_t>
(C++20)std::basic_string_view<char8_t>
(C++17)std::basic_string_view<char16_t>
(C++17)std::basic_string_view<char32_t>Template parameters
CharT - character type Traits -
class specifying the operations on the character type. Like for
, Traits::char_type must name the same type as CharT or the program is ill-formed. Nested types
Type Definition traits_typeTraitsvalue_typeCharTpointerCharT*const_pointerconstCharT*referenceCharT&const_referenceconstCharT&const_iteratorimplementation-defined constant
, whose value_type is CharT
iteratorconst_iteratorconst_reverse_iteratorstd::reverse_iterator<const_iterator>reverse_iteratorconst_reverse_iteratorsize_type
difference_type
Note: iterator and const_iterator are the same type because string views are views into constant character sequences.
All requirements on the iterator types of a
applies to the iterator and const_iterator types of basic_string_view as well.
Data members
Member Description const_pointerdata_a pointer to the underlying sequence
(exposition-only member object*)size_typesize_the number of characters
(exposition-only member object*)Member functions
Constructors and assignment
constructs a basic_string_view
(public member function)
assigns a view
(public member function)
Iterators
returns an iterator to the beginning
(public member function)
returns an iterator to the end
(public member function)
returns a reverse iterator to the beginning
(public member function)
returns a reverse iterator to the end
(public member function)
Element access
accesses the specified character
(public member function)
accesses the specified character with bounds checking
(public member function)
accesses the first character
(public member function)
accesses the last character
(public member function)
returns a pointer to the first character of a view
(public member function)
Capacity
returns the number of characters
(public member function)
returns the maximum number of characters
(public member function)
checks whether the view is empty
(public member function)
Modifiers
shrinks the view by moving its start forward
(public member function)
shrinks the view by moving its end backward
(public member function)
swaps the contents
(public member function)
Operations
copies characters
(public member function)
returns a substring
(public member function)
(C++26)
returns a sub-view
(public member function)
compares two views
(public member function)
(C++20)
checks if the string view starts with the given prefix
(public member function)
(C++20)
checks if the string view ends with the given suffix
(public member function)
(C++23)
checks if the string view contains the given substring or character
(public member function)
find characters in the view
(public member function)
find the last occurrence of a substring
(public member function)
find first occurrence of characters
(public member function)
find last occurrence of characters
(public member function)
find first absence of characters
(public member function)
find last absence of characters
(public member function)
Constants
[static]
special value. The exact meaning depends on the context
(public static member constant)
Non-member functions
Literals
Helper classes
Helper templates
template<classCharT,classTraits>inlineconstexprboolranges::enable_borrowed_range<std::basic_string_view<CharT,Traits>>=true;(since C++20)This specialization of
makes basic_string_view satisfy
.
template<classCharT,classTraits>inlineconstexprboolranges::enable_view<std::basic_string_view<CharT,Traits>>=true;(since C++20)This specialization of
makes basic_string_view satisfy
.
Notes
It is the programmer's responsibility to ensure that std::string_view does not outlive the pointed-to character array:
std::string_viewgood{"a string literal"};// "Good" case: `good` points to a static array.// String literals reside in persistent data storage.std::string_viewbad{"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.
macro ValueStdFeature
(C++17)
(C++20)
(C++23)
Example
Run this code
#include<iostream>#include<string_view>intmain(){#define A "▀"#define B "▄"#define C "─"constexprstd::string_viewblocks[]{ABC,BAC,ACB,BCA};for(inty{},p{};y!=8;++y,p=((p+1)%4)){for(charx{};x!=29;++x)std::cout<<blocks[p];std::cout<<'\n';}}Output:
▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─ ▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─ ▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄ ▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀ ▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─ ▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─ ▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄ ▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀ Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++17 only pointers, iterators, and references
returned from the member functions of
basic_string_view might be invalidated all pointers, iterators, and references
to elements of basic_string_view
may be invalidated See also