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

215 questions
0
votes
1 answer

Is there really no explicit constructor of std::string from an std::string_view?

Some (many?) programmers who are introduced to both std::string_view and std::string ask themselves: "Why can I convert the latter into the former, but not the other way around?" One part of the question is answered here: Why is there no implicit…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
1 answer

Substring console output using std::string_view

Is it possible to substring console output of an std::string using std::string_view? For example: std::string toolong {"this is a string too long for me"}; std::string_view(toolong); // do something... expected console output: this is a string
Sergio
  • 891
  • 9
  • 27
0
votes
2 answers

Erase inside a std::string by std::string_view

I need to find and then erase a portion of a string (a substring). string_view seems such a good idea, but I cannot make it work with string::erase: // guaranteed to return a view into `str` auto gimme_gimme_gimme(const std::string& str) ->…
bolov
  • 72,283
  • 15
  • 145
  • 224
0
votes
1 answer

Remove last character of std::string_view

I'm trying to remove the last character of an std string view, but no matter what I do it remains there. I think its because I'm accidentally removing the "/0" instead of the desired "]". Here is my code: #include #include…
Blue7
  • 1,750
  • 4
  • 31
  • 55
0
votes
1 answer

How to remove a substring from a string_view at compile time?

I created a macro named DBG, which prints an expression itself and the value it evaluates to. So DBG(5+1) should print 5+1 = 6. That macro works fine. Yet, if I encapsulate more than one of those macro, that becomes quite unreadible, because the…
Jack Sabbath
  • 1,398
  • 2
  • 9
  • 12
0
votes
2 answers

File to std::string_view

Is it possible to load file directly into the std::string_view? Directly = without creating the proxy std::string from stringstream. It would make a lot of my code faster.
JWZ1996
  • 77
  • 9
0
votes
0 answers

Why does std::basic_string_view support only const pointers?

According to the description in cppreference.com: The class template basic_string_view describes an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero. However,…
Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
0
votes
1 answer

string_view backed by a given string

In C++, suppose I extract lines from a stream, mimicking getline(). Each time I extract a line, using e.g. low-level primitives, I would also like to create a vector of string_views backed by the extracted string. So, essentially, my method would…
Ilonpilaaja
  • 1,169
  • 2
  • 15
  • 26
0
votes
1 answer

null-terminated string_view wanted

Using std::string_view in following scenario: struct A : public std::exception{ A (const char* c) : v_(c){} const char* what() const noexcept override; private: std::string_view v_; }; The above idea works fine and now the copy-ctor…
darune
  • 10,480
  • 2
  • 24
  • 62
0
votes
0 answers

Building a wrapper class around around std::string AND std::string_view

I have a C++ app with deep, refined logic developed over many years. Sadly, it combines all manner of different types of strings: std::string, C-strings, Pascal strings, and OS/platform-specific strings. Plus, each of these string types often use…
SMGreenfield
  • 1,680
  • 19
  • 35
0
votes
1 answer

Comparing a std::string_view and a substring string_view

Let's say that we have a string_view and another string_view that is a subset of the first string_view: using namespace std; // just to shorten the example... string_view s{"abc def"}; auto t = s.substr(4); auto u = s.substr(0, 4); cout <<…
Domen Vrankar
  • 1,743
  • 15
  • 19
0
votes
0 answers

What is the significance (or purpose) of this trailing s in this std::string_view example?

Update: The answer(s) and discussion provided in the existing answer are more than sufficient to satisfy this question. If you have a similar question the suggested reading is this paper on user-defined literals and the…
eric
  • 1,029
  • 1
  • 8
  • 9
0
votes
1 answer

string_view with a deallocator

I'm trying to use std::string_view as much as I can to wrap C-string, however, whenever the C-string I'm wrapping is dynamically allocated, I rely on this pattern: char *cs = get_c_string(); std::string s(cs); free(cs); which is a waste of time…
Benoît
  • 3,355
  • 2
  • 29
  • 34
0
votes
0 answers

Is constructing string_view with an invalid pointer undefined behavior?

In C++ Extensions for Library Fundamentals, Version 2, it is stated that, for string_view constructor: constexpr basic_string_view(const charT* str, size_type len); Requires: [str,str + len) is a valid range. But it is not defined what a “valid…
Serdar Sanli
  • 1,064
  • 1
  • 11
  • 20
0
votes
1 answer

c++ boost::iterator_range string_view error

I am trying to split a string the fastest way possible in C++. I am getting an error here: #include #include #include #include #include…
SkyRipper
  • 155
  • 5
  • 15
1 2 3
14
15