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…
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
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) ->…
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…
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…
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.
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,…
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…
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…
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…
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 <<…
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…
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…
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…