std::basic_string_view (3) - Linux Manuals

std::basic_string_view: std::basic_string_view


std::basic_string_view - std::basic_string_view


Defined in header <string_view>
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 char-like objects with the first element of the sequence at position zero.
A typical implementation holds only two members: a pointer to constant CharT and a size.
Several typedefs for common character types are provided:

Defined in header <string_view>
Type Definition
std::string_view std::basic_string_view<char>
std::wstring_view std::basic_string_view<wchar_t>
std::u8string_view std::basic_string_view<char8_t> (C++20)
std::u16string_view std::basic_string_view<char16_t>
std::u32string_view std::basic_string_view<char32_t>

Template parameters

CharT - character type
Traits - CharTraits class specifying the operations on the character type. Like for basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed.

Member types

Member type Definition
traits_type Traits
value_type CharT
pointer CharT*
const_pointer const CharT*
reference CharT&
const_reference const CharT&
                       implementation-defined constant LegacyRandomAccessIterator
const_iterator , ConstexprIterator
                       (since C++20) and LegacyContiguousIterator whose value_type is CharT
iterator const_iterator
reverse_iterator const_reverse_iterator
const_reverse_iterator std::reverse_iterator<const_iterator>
size_type std::size_t
difference_type std::ptrdiff_t

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 Container applies to the iterator and const_iterator types of basic_string_view as well.

Member functions

                  constructs a basic_string_view
constructor (public member function)
                  assigns a view
operator= (public member function)


                  returns an iterator to the beginning
begin (public member function)
                  returns an iterator to the end
end (public member function)
                  returns a reverse iterator to the beginning
rbegin (public member function)
                  returns a reverse iterator to the end
rend (public member function)

Element access

                  accesses the specified character
operator[] (public member function)
                  accesses the specified character with bounds checking
at (public member function)
                  accesses the first character
front (public member function)
                  accesses the last character
back (public member function)
                  returns a pointer to the first character of a view
data (public member function)


                  returns the number of characters
size (public member function)
                  returns the maximum number of characters
max_size (public member function)
                  checks whether the view is empty
empty (public member function)


                  shrinks the view by moving its start forward
remove_prefix (public member function)
                  shrinks the view by moving its end backward
remove_suffix (public member function)
                  swaps the contents
swap (public member function)


                  copies characters
copy (public member function)
                  returns a substring
substr (public member function)
                  compares two views
compare (public member function)

starts_with checks if the string view starts with the given prefix
                  (public member function)

ends_with checks if the string view ends with the given suffix
                  (public member function)
                  find characters in the view
find (public member function)
                  find the last occurrence of a substring
rfind (public member function)
                  find first occurrence of characters
find_first_of (public member function)
                  find last occurrence of characters
find_last_of (public member function)
                  find first absence of characters
find_first_not_of (public member function)
                  find last absence of characters
find_last_not_of (public member function)


npos special value. The exact meaning depends on the context
                  (public static member constant)

Non-member functions

operator!= lexicographically compares two string views
operator< (function template)

begin returns iterators to the beginning and the end of the string view
end (function)



           performs stream output on string views
operator<< (function template)


Defined in inline namespace std::literals::string_view_literals

operator""sv Creates a string view of a character array literal

Helper classes

std::hash<std::u32string_view> hash support for string views
                               (class template specialization)


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"); // OK: "good" points to a static array
  std::string_view bad("a temporary string"s); // "bad" holds a dangling pointer