What is the difference between "string_view" for "string" and which one should you prefer?

2

What's the difference between the new C ++ class and 17 string_view for string ?

In what situations is the use of string_view indicated instead of string ?

    
asked by anonymous 22.08.2018 / 20:22

1 answer

2

The string_view is a simple reference to a string . The latter has an object with data that is a string of characters. The first one has a pointer to string and how many characters is its size.

The pointer does not have to be for the first character and quantity does not need to catch all the following characters. So it's a form of substring but it does not copy the data to another object, and does not take ownership of the original object. It's a way to keep access more efficient.

The string_view object does not allow you to make changes to the object it references, so it is considered immutable and has no concurrency problems.

When you need to reference the string object or part of it without needing to change it or have something separate it is more interesting.

Did some tests :

Butitdependsonimplementation.Notethatdependingonthecompilerthesamesplitoperationmaybebetterorworse.ThefirstusesGCCandthesecondusesClang.

Documentation .

    
22.08.2018 / 20:35