Javatpoint Logo
Javatpoint Logo

String_view in C++

Introduction:

The default method of interacting with strings in C++ is called std::string since it provides users with a wide range of useful functionalities. Among many other string operations, std::string offers string operations, including looking for substrings, comparing strings, concatenating strings, and slicing strings. But each time an action is done on the string, std::string necessitates the dynamic allocation of more memory as well as additional buffer space.

Define string_view.

String_view can only be used to view a string; it cannot be used to change the string itself conceptually. Unlike when you create a copy of a string, there is no need to copy the data when a string_view is created. It is also less than std::string in terms of heap space. It is only available in the C++ 17 version, std::string_view cannot change the underlying data.

The lightweight std::string_view object simply refers to the contiguous char sequence and gives read-only access to a string or a portion of a string using an interface that is comparable to the std::_string interface. It offers a view of a string that is defined elsewhere in the source code, unlike std::string, which maintains its own copy of the string.

It has two members:

the _size and a const char* that points to the beginning of the char array. It refers to a string that doesn't belong to anyone. It is specified in the header (#include "string_view") and std::.

Why would we use std::string_view?

There are various reason that would requires the string_view. Some of them are as follows:

  1. When you wish to eliminate extra copies, string_view is helpful.
  2. Building and copying String_views requires less memory. String_view doesn't need a dynamic allocation when it is created from literals.
  3. In the same way, std::string::substr returns a new string when producing a substring, which may need a dynamic allocation. In order to prevent any more memory from being dynamically allocated, we can create a string_view from the address of a specific location in our string.

Syntax

The following syntax is used to declare a string_view object:

By doing this, the string "Hello, world!" is referenced by a string_view object.

Members

The following individuals create for the string_view class:

  1. data: A reference to the string's beginning.
  2. size: It is used to determine the string's width.

Methods

These are the methods available in the string_view class:

begin():

It gives a pointer to the string's beginning as a response.

end():

It gives a pointer to the string's end.

length():

It gives the string's size back.

size_t start, size_t end, substr:

It is a new string_view object that makes reference to a portion of the current string is returned.

compare(const std::string_view& other):

It compared to another string_view object the current string.

equals(const std::string_view& other):

If the current string matches another string_view object, it returns true.

find(char c):

It gives the string's first instance of the provided character's index.

rfind(char c):

It gives the string's last instance of the provided character's index.

contains(char c):

If the requested character is present in the string, a true value is returned.

Why the use of std::string_view comes in actually:

Let's see why and how we are making use of std::string_view practically by taking some examples;

Constant strings are one of the most frequent uses for the std::string's drawbacks. This program illustrates the issue that arises when using std::string to handle constant strings:

Output:

Hello !, JavaTpoint
Hello !, JavaTpoint
Hello !, JavaTpoint

Explanation:

The results match what was predicted. But the std::string executes memory overheads twice in order to display "Hello!, JavaTpoint" twice. However, in this case, reading the text ("Hello!, JavaTpoint") was necessary; no operation to write to it was needed. So why would you repeatedly assign memory only to display a string? C++17 proposed std::string_view(), which gives the view of pre-defined char str[] without adding a new object to the memory, to deal with strings more effectively.

The problem with std::string

By assigning two different string variables, the identical string, str_1, is written twice in the above example. Therefore, the memory was allocated using the static memory allocation for both variables str_2 and str_3, which double-overhead on our memory.

Advantages of using std::string_view

There are various advantages of std::string_view. Some main advantages of this function are as follows:

Cheaper and lighter:

The fundamental purpose of the lightweight, less expensive std::string_view is to offer the view of the string. There is no need to replicate the string in the inefficient way that was done in the aforementioned example and that was adding overhead to the RAM whenever the string_view is formed. When a change is made to the watched string, the changes are immediately reflected in the std::string_view, which greatly improves the efficiency of the string copying process.

Improved Efficiency:

The std::string_view is superior to the const std::string& because it eliminates the requirement that a std::string object be present at the very beginning of the string. The std::string_view is made up of two elements, the first of which is a const char* that designates the array's starting position and the second of which is size.

Supports an important function

The majority of essential operations performed on a std::string, such as substr, compare, find, and overloaded comparison operators (such as ==,, >, and!=), are supported by the std::string_view. Since read-only is our choice, it typically removes the requirement for a std::string object declaration.

Here is the exact source code for the previously mentioned program using std::string_view:

Output:

Hello !, JavaTpoint
Hello !, JavaTpoint
Hello !, JavaTpoint

Explanation:

The result would be identical to that seen above, but no more copies of the string "Hello!, JavaTpoint" would be produced in memory.

The C++ program to demonstrate the Char Type is below:

Output:

13
Hello, world!

Std::string_view Example:

The below example demonstrates how most string-based functions can be used in conjunction with std::string_view, the functions str.compare(), str.back(), str.cend(), and str.at().

Output:

The strings are equal.
The last character of the string is !
The end pointer of the string is 
The character at index 5 is ,






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA