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