Questions tagged [string-view]

A string_view is a C++ class template that is a non-owning reference to a string.

Reference: http://en.cppreference.com/w/cpp/experimental/basic_string_view

138 questions
13
votes
1 answer

How to split a std::string into a range (v3) of std::string_views?

I need to split a std::string at all spaces. The resulting range should however transform it's element to std::string_views. I'm struggling with the "element type" of the range. I guess, the type is something like a c_str. How can I transform the…
dani
  • 2,917
  • 3
  • 19
  • 43
13
votes
1 answer

string_view behaviour when passing temporary std::string

I just ran into some misunderstanding: at least in libc++ implementation std::experimental::string_view has the following concise implementation: template class basic_string_view { public: typedef _CharT…
Bikineev
  • 1,645
  • 14
  • 20
12
votes
2 answers

When do you need a null terminated string in a read-only scenario?

I've been playing around with the std::string_view library and I have been contemplating on changing a code base I have been working on to use std::string_view as much as possible. However, in many of the threads that I have read on the subject of…
Sailanarmo
  • 996
  • 8
  • 29
12
votes
2 answers

Differences between boost::string_ref and boost::string_view

Boost provides two different implementations of string_view, which will be a part of C++17: boost::string_ref in utility/string_ref.hpp boost::string_view in core/string_view.hpp Are there any significant differences between these? Which should be…
leecbaker
  • 3,283
  • 2
  • 31
  • 48
11
votes
3 answers

How to correctly create std::string from a std::string_view?

I have a class: class Symbol_t { public: Symbol_t( const char* rawName ) { memcpy( m_V, rawName, 6 * sizeof( char ) ); }; string_view strVw() const { return string_view( m_V, 6 ); }; private: char m_V[6]; }; // class…
Leon
  • 897
  • 4
  • 20
11
votes
1 answer

Implementation of string_view formatted stream ouput

While implementing C++1z's std::basic_string_view to use it on older compilers, I encountered a problem with the stream output operator overload for it. Basically, it has to output the contents referenced by the string_view while not relying on any…
nshct
  • 1,147
  • 8
  • 28
9
votes
0 answers

Using string_view to search unordered_map

I need to extract all substring of a certain size from a given string and look-up each of them in a std::unordered_map. I tried to use the suggestion from this answer and used std::less<> as the comparator but the compiler (gcc 8.2)…
DarthPaghius
  • 723
  • 1
  • 6
  • 24
9
votes
2 answers

c++17 Ambiguity when compare string_view with string

I saw both std::string_view and std::string have symmetric operator==() and for std::string it has the constructor to accept std::string_view and operator to convert itself to std::string_view. So when we try to use operator==() compare between…
9
votes
1 answer

std::string_view and std::string in std::unordered_set

Let's say you have an std::unordered_set . You have an std::string_view object that you want to search for in the container. Problem is, you don't want to create a std::string from your std::string_view, as this kind of defeats the…
Adam
  • 605
  • 4
  • 11
8
votes
3 answers

Why does std::string_view::data not include a null terminator?

This code has undefined behavior: #include #include using namespace std::string_view_literals; void foo(std::string_view msg) { std::cout << msg.data() << '\n'; // undefined behavior if 'msg' is not null- …
Rakete1111
  • 42,521
  • 11
  • 108
  • 141
8
votes
3 answers

Why string_view constructor doesn't take a pair of iterators

Both string_ref in boost and string_span in GSL doesn't define constructor that takes a pair of iterator. What is the reason of this decision ? Usually it's not a big deal, I can just create string_ref like this : boost::string_ref s(start,…
Kamil Zubair
  • 201
  • 1
  • 9
7
votes
1 answer

Constructing a string_view from a range of chars

While a span can be constructed from a range, a string_view cannot be constructed from a range of chars. Thus, for example, the following code is required: // assume chars_span is a span of chars std::cout << std::string_view(chars_span.data(),…
Amir Kirsh
  • 8,021
  • 22
  • 43
6
votes
1 answer

Create span of string_views from C string array

Given some function void func(std::span), how does one feed this function a raw array of C-strings const char** in the most efficient manner? As far as I understood this should be possible as without any copying as std::string_view…
Joel Bodenmann
  • 1,776
  • 2
  • 11
  • 29
6
votes
1 answer

Backwards compatability considerations when moving from const std::string& to std::string_view?

I maintain a C++ library that frequently uses const std::string& arguments in its API. However, I have received some user requests to switch over to std::string_view to help enable efficiencies that wouldn't be possible with the current API. I am…
Lalaland
  • 8,180
  • 3
  • 30
  • 47
6
votes
1 answer

How to pass std::string_view by value or by const reference

Usually string_view is used for function parameters like this: void fval(std::string_view sv); void fcref(std::string_view const &sv); Which is better? const reference is 8 bytes and string_view is usually twice that, e.g. 16 bytes. However, if not…
Nick
  • 8,479
  • 2
  • 36
  • 64
1
2
3
9 10