Search⌘ K
AI Features

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...

Introductory Example

Let’s try a little experiment:

How many string copies are c​reated 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 ...