Achieve High Performance By Using The string_view C++17 Feature In C++Builder

In this tutorial, you will learn another modern C++17 feature to work with strings. This feature is a std::string_view. The purpose of any kinds of string reference proposals is to bypass copying data that already owned someplace and of which only a non-mutating representation is required. The std::string_view is one such proposal. There was an earlier one named string_ref.

The std::string_view is a picture of the string and cannot​ be utilized to alter the original string value. When a std::string_view is constructed, there’s no need to replicate the data. Besides, the std::string_view is smaller than std::string on the heap.

How can you use std::string_view with C++ Builder?

  • string_view lives in the header file

Benefits of the string_view

  • string_view is useful when you want to avoid unnecessary duplicates
  • The creation of string_view from literals does not need a dynamic allocation.

The following code illustrates how string_view supports save memory by restricting unnecessary dynamic allocations:

So, std::string_view is an extraordinary utility for great performance. But, the programmer must assure that std::string_view does not outlive the pointed-to character array.

Additional functions provided by std::string_view can be found here, on the official documentation.

Head over and check out the Windows string_view demo for C++Builder on GitHub.