The Basics
Let's begin our study on std::string_view with a simple use case.
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?
C++ 17
// string function:std::string StartFromWordStr(const std::string& strArg, const std::string& word) {return strArg.substr(strArg.find(word)); // substr creates a new string}int main() {// call:std::string str {"Hello Amazing Programming Environment" };auto subStr = StartFromWordStr(str, "Programming Environment");std::cout << subStr << '\n';}
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