I need to split a std::string
at all spaces. The resulting range should however transform it's element to std::string_view
s. 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 "split"-part into string_view
s?
#include <string>
#include <string_view>
#include "range/v3/all.hpp"
int main()
{
std::string s = "this should be split into string_views";
auto view = s
| ranges::view::split(' ')
| ranges::view::transform(std::string_view);
}
(One of) the problem here is that ranges::view::split
returns a range of ranges, and you cannot construct a std::string_view
directly from a range.
You want something like this:
auto view = s
| ranges::views::split(' ')
| ranges::views::transform([](auto &&rng) {
return std::string_view(&*rng.begin(), ranges::distance(rng));
});
There might be a better/easier way to do this but:
&*rng.begin()
will give you the address of the first character of the chunk in the original string.ranges::distance(rng)
will give you the number of characters in this chunk. Note that this is slower than ranges::size
but required here because we cannot retrieve the size of rng
in constant time.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With