Search⌘ K
AI Features

Other Operations

Learn how to use the various operations of std::string_view introduced in C++17. This lesson covers iterators, element access, size and capacity methods, modifiers, and comparison functions, helping you handle strings efficiently without ownership while maintaining read-only access.

string_view is modelled to be very similar to std::string. The view, however, is non-owning, so any operation that modifies the data cannot go into the API. Here’s a brief list of methods that you can use with this new type:

Iterators

Method Description
cbegin(), begin() Return an iterator to the first character
crbegin(), rbegin() Return a reverse iterator to the first character of the reversed view. It corresponds to the last character of the sequence.
cend(), end() Returns an iterator to a place after the last character of a sequence
crend(), rend() Returns an iterator to the end of reversed sequence. It corresponds to a place before the first character

Note: all of the above methods are constexpr and const, so you always get a const iterator (even for begin() or end()). ...

Accessing Elements

Method Description
operator[] Returns a const reference to the character at the specified position. Bounds