The Basics
Explore the fundamentals of std string_view in C++17 to improve string handling efficiency by avoiding unnecessary copies and allocations. Understand how string_view provides a non-owning view of strings, enabling safer and faster string operations in your code.
We'll cover the following...
We'll cover the following...
Introductory Example
Let’s try a little experiment:
How many string copies are created in the below example?
Can you count them all?
The answer is 3 or 5 depending on the compiler, but usually, it should be 3.
- The first one is for
str. - The second one is for the second argument in
StartFromWordStr- the argument isconst string&so since we passconst char*it will create a new string. - The third one comes from
substr