C++ Weekly - Ep 340 - Finally! A Simple String Split in C++!

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

FlexiSpot Pro Plus Standing Desk E7
#MyFlexiDesk #flexiblewfh

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

There's some very nice aspects of C++, and it's been my daily driver for a lot of years - but the fact that it's 2022 and splitting a string _still_ looks like the dog's dinner, and even after all that you have to jump through hoops to use the chunks you get back does say something about it... ;)

fukawitribe
Автор

I have the urge to applaud you for the respectful way you did the ad!

dmdjt
Автор

I usually split using string_view anyway, nice to see ranges has such an elegant solution!

N....
Автор

I still can not understand why the C++ members seem so opposed to a simple stringsplit/concatenate method.

ABaumstumpf
Автор

This is great. You getting sponsorships will hopefully lead to more videos, I hope.

anon_y_mousse
Автор

Most languages' definition of simple:

print "hello world".split(" ")

C++ definition of simple:

constexpr static auto str = std::string_view{"hello world"} | std::ranges::views::split(' ');
for (const auto &string : auto{split_strings}) {
std::cout << std::string_view{string};
}

prahTV
Автор

It is still far from easy! This would be easy: auto splitted = std::splitt(string, " ");
And a "range of ranges" is not a standard container. The output should be std::vector<std::string> or similar.

mocloun
Автор

tell me more use cases for constexpr string views please

lucasrangit
Автор

That auto{} in the for loop that "makes the copy explicit" is completely new for me. What's the name of that feature?

nites
Автор

I remember also adding my answer to another question asking about string splitting in c++, on stackoverflow.
My answer was lazy in nature because it created an iterator over the tokens rather than forcing one to specify a container to collect the tokens into, and the tokens are generated as the iterator is consumed.

I was as surprised then (about 8 years ago) as I am now, that something this simple is still an issue for C++.
I can't wait until Carbon becomes mature, I bet they already have a neat solution in mind.

chigozie
Автор

Would ‘constinit’ make sense for that ‘split_strings’?

Xilefian
Автор

what if words are separated by multiple spaces? How does it handle it? Does it filter out empty ranges or you have to do it yourself?

yato
Автор

Splitting strings is still stress in C++ but at least this a good approach to solve it.

v-for-victory
Автор

I find the Matlab style quite friendly. To all experts, why porting a similar library with such simplicity has not been done? Any caveats I'm missing?

franciscogerardohernandezr
Автор

This might be useful for better way for parsing a whole text document and contain it for indexing and mapping.

Key_Capz_
Автор

Finally! Lol. But can we also split based on other delimiters and not just space or tab characters?

ohwow
Автор

How does a "range of ranges" actually work? Since it can't know how many views there will be, it must allocate dynamic memory, right? It's essentially like a vector of ranges?

nothke
Автор

i personally very rarely use std::string, using my own class, which has a split method XD

ChaotikmindSrc
Автор

The 'splits' I get from this are string_views and not strings. They are invalidated as soon as the input string is modified/destroyed (without warnings!). I still need to keep track of the data myself. If you want to keep the splits as members in a class then you loose the copy-/move-ctors unless you keep the data outside the object or you manually make new strings from the string_views. I believe the use of string_views to be quite limited, it would be nice if they were more than just pointers into a string (or less = just offsets not tied to a string instead of pointers). I'd rather be able to store string_views inside the string so that they stay copyable/moveable.

progammler
Автор

still looking for the video where will be chiming in with take on Google's Carbon language (such as it is right now)

TheSulross