class std::string_view in C++17


C++ contains many precious features to enhance the performance of the code, and string_view class is one of them. It is introduced to create a lightweight and non-owning reference to a string. In this tutorial, we will discuss the string_view class and explore a few examples using the string_view class over the string class in C++.

What is string_view?

The string_view is a class in C++ that is used to create a read-only sequence of the string. It is a non-owning string type, meaning it does not manage the memory associated with the string and its reference. It acts as a lightweight wrapper over the string, allowing us to access the string efficiently without creating multiple copies.

Basically, we can use the string_view class to define the string when we need to create a string to read, but we don’t need to do any modification.

Let’s understand the requirements of using the string_view class via the examples below.

Example 1 (Using the string class)

In the example below, we created the string1, string2, and string3. Also, we have initialized the string2 and string3 with string1. In the output, we can observe the value of the string1, string2, and string3.

#include <iostream>
#include <string>

using namespace std;
int main() {
   char string1[]{"Welcome to the TutorialsPoint!"};
   string string2{string1};
   string string3{string1};
   // Show the strings in the output
   cout << string1 << endl;
   cout << string2 << endl;
   cout << string3 << endl;
   return 0;
}

Output

Welcome to the TutorialsPoint!
Welcome to the TutorialsPoint!
Welcome to the TutorialsPoint!

The above code prints the different strings 3 times. Also, we don’t require to modify all 3 strings in the above code as we read them only. Still, we created three different strings, which take a compiler memory.

So, we can use the string_view class so that it won't allocate memory to the reference of the particular string.

Example 2 (Using the string_view class)

In this example, we created string1 and gave its reference to the string2 and string1. Also, we used the string_view class to create a string. Next, we printed all three strings in the output.

#include <iostream>
#include <string_view>
using namespace std;

int main() {
   string_view string1{"Welcome to the TutorialsPoint!"};
   string_view string2{string1};
   string_view string3{string1};
   // Show the strings in the output
   cout << string1 << endl;
   cout << string2 << endl;
   cout << string3 << endl;
   return 0;
}

Output

Welcome to the TutorialsPoint!
Welcome to the TutorialsPoint!
Welcome to the TutorialsPoint!

Example 2 gives the same output as example 1, but example 2 doesn’t create 3 copies of the same string in the memory as example 1. So, we can say that the string_view class increases the performance of the code as it takes less memory.

Example 3

In the example below, we check whether two strings are equal. We used the string_view class to define the string, as we needed to read the string to match both strings.

The areStringsEqual() function returns the boolean value based on whether strings are equal.

 #include <iostream>
#include <string_view>

using namespace std;
bool areStringsEqual(string_view string1, string_view string2) {
    return string1 == string2;
}
int main() {
   string_view string1("Tutorials");
   string_view string2("Point");
   if (areStringsEqual(string1, string2))
      cout << "Strings are equal." << std::endl;
   else
      cout << "Strings are not equal." << std::endl;
   return 0;
}

Output

Strings are not equal.

Example 4

In the example below, we find the substring of the given string. We defined the strings using the string_view class as we need to read strings to get substring, but we don’t need to modify the strings.

In the printSubstring() method, we use the substr() method to get the substring from the given positions and print them.

 #include <iostream>
#include <string_view>

using namespace std;
void printSubstring(string_view str, int startPos, int length) {
   string_view substring = str.substr(startPos, length);
   cout << "Substring is : " << substring << endl;
}
int main() {
   string_view sentence("Hey! Hi How are you doing these days?");
   printSubstring(sentence, 4, 5); 
   printSubstring(sentence, 16, 5);
   printSubstring(sentence, 36, 3); 
   return 0;
}

Output

Substring is :  Hi H
Substring is : you d
Substring is : ?

What are the benefits of using string_view?

Here we have written some benefits of using the string_view class.

  • Efficient string handling − The string_view doesn’t copy the string data. It allows developers to pass and store the references to the string without the cost of memory allocation and deallocation. It saves memory.

  • Non-modifiable − We can use the string_view class when we need to create only readable strings and ensure that they shouldn’t be modified by anyone unintentionally. Generally, we can use it when we need to define the string and pass it as a function parameter to use inside the function.

  • Compatibility − We can use the string_view class easily in the code as we use the string class. Also, we can convert between the string and string_view without making much effort

We have seen the benefits of using the string_view class over the string class in the C++ code.

Conclusion

In this tutorial, we learned to use the string_view class in C++. We should always use the string_view class when we need to create only readable strings to avoid unintentional modification. Also, it helps us improve the code's performance and is as easy to use as a string class.

Updated on: 18-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements