In this tutorial i read that i should use std::string_view instead of C-style strings, but i didn't figure out why. String_view has many problems, such as null-terminator, so it looks useful only while passing as function parameter instead std:string.
-
5Could you share your knowledge about those many problems? What is wrong with null-terminator? – 273K Jul 21 '22 at 01:25
-
4Please explain why `string_view` has a "problems" with null-terminator. The point of this class is to wrap a C-style string with a `std::string` interface which is very useful, especially for things like using a table of string literals as the key for a map without creating extra strings. – paddy Jul 21 '22 at 01:26
-
4Excerpt from the linked tutorial: _"Unlike C-style strings and std::string, std::string_view doesn’t use null terminators to mark the end of the string. Rather, it knows where the string ends because it keeps track of its length."_ -- this is actually good news. It means that it is even _more_ flexible. The only issue I can see there is that if you use it with non-terminated strings then trying to access a string_view in a function that _expects_ terminated strings you will have a problem. – paddy Jul 21 '22 at 01:30
-
`string_view` is applicable for passing as function parameter (that is already quite useful). It is a view so it is not used for storing data itself. What other uses are you interested in? Pointer/reference/view to the inside of strings as intermediate results, e.g. for searching/parsing through a string? Templating other types, e.g. `map`? Storing pointers or references to strings external to the class as member variable? `string_view` does not replace `string`. Both have their uses. `string_view` can contain a trailing NUL character, too, if you choose so and can guarantee it for your code. – Sebastian Jul 21 '22 at 03:19
-
@paddy if you try to use a string_view as `const char *` on something that is a substring you will overshoot. Even if it eventually finds the `0` of the original string it would still give wrong results. – Goswin von Brederlow Jul 21 '22 at 21:21
-
Any `const char *` you pass around should be a `std::string`. Except when they are `std::string_view`. So isn't that perfect? You should pass `std::string` when you want to store the value and switch to `string_view` whenever you do any divide&conquer or substrings. – Goswin von Brederlow Jul 21 '22 at 21:24
-
You can use `zstring_view`, see e.g. https://stackoverflow.com/questions/41286898/using-stdstring-view-with-api-what-expects-null-terminated-string/41288372#41288372 There are several implementations around, if you google `zstring_view`. The GSL also contains `basic_zstring`, `zstring`, `czstring`, ... – Sebastian Jul 22 '22 at 05:29
1 Answers
After using the C++ library for a period of time you will begin to notice things which keep happening over, and over, and over again, and you will become very familiar with them, and consider them your best friends forever.
For example, no matter where you look, be it a std::string
, a std::vector
, a std::list
, or any other container, you'll always see: a begin()
, an end()
, and many other things they also share in common. There will be some exceptions, not everyone has a lower_bound()
but most do, for example. So, you'll still think of lower_bound()
as one of your best friends.
You will also begin to notice an amazing thing about many algorithms in the C++ library, like std::sort
. They'll happily accept what comes out from begin()
and an end()
, be it a string's, a vector, or a std::array
's. They're also a part of the same family. Everyone knows each other.
Now, you come to a std::string_view
. Guess what, a std::string_view
also looks like familiar territory. It fits right in, with all of your other friends. It works just like (most of) the other things you are already familiar with, when working with the rest of the C++ library.
But here's this const char *
thing. What is this weird, strange, bizarre creature??? It doesn't look like anything you know. It has no begin()
, or end()
, or anything what everyone has. What an odd, peculiar thing this is... It should belong in some zoo, somewhere, rather than the C++ library.

- 114,536
- 5
- 94
- 148