Introduction

The string view class builds itself on the string class. It restricts the operations that can be performed on a string.

A string view is a non-owning reference to a string. It represents a view of a sequence of characters. This sequence of characters can be a C++ string or a C-string. A string view needs the header <string_view>.

🔑 A string view is a for copying optimized string
From a birds-eye perspective the purpose of std::string_view is to avoid copying data that is already owned by someone else and to allow immutable access to a std::string like object. The string view is a kind of a restricted string that supports only immutable operations. Additionally, a string view sv has two additional mutating operations: sv.remove_prefix and sv.remove_suffix.

String views are class templates parameterized by their character and their character trait. The character trait has a default. In contrast to a string, a string view is non-owner and, therefore, needs no allocator.

Get hands-on with 1000+ tech skills courses.