4 ms·
It is not difficult to do it, but it is difficult to do right and in a C++ way. That means with least overhead, i.e. not copying stuff if not necessary and not
by seppel 6y ago
It is not difficult to do it, but it is difficult to do right and in a C++ way. That means with least overhead, i.e. not copying stuff if not necessary and not doing any memory allocations. This is why C++ still has no string split is its standard libary: People cannot agree how it should look like.
There is, however, boost::split from the famous boost library.
- mehrdadn 6y agoDo you know if they're revisited this ever since string_view was implemented? I feel like a vector<string_view> should be quite uncontroversial, and you could even propagate the allocator from the string to the vector.
- seppel 6y agoSince they now have ranges, the discussion can start all over... But I'm maybe no completely up to date, where the discussions is.
- makapuf 6y agoWith range it would be auto splitText = text | view::split(' ') | ranges::to<std::vector<std::<wbr />string>>();
- deleted 6y ago[deleted]
- leni536 6y agoIt should return a range of string views, no allocation is necessary.
- mehrdadn 6y agoThat sounds like a great idea. Not sure if it might present a bit of a hurdle if the caller prefers it to be random-access, but I'm kind of tempted to try this out and see if it might present issues in practice. It might be worth suggesting it to the standards committee?
- leni536 6y agoI think it should be bidirectional. Random access is expected to mean O(1) advancing of the iterator. I don't think it can be done without upfront work and allocation at construction. If one needs random access they can always do the bidirectional range -> vector of string_views conversion. Ranges are relatively new in the standard and not many algorithms landed in the library. I'm pretty sure there will be many new range algorithms in the upcoming standards. This particular algorithm is probably worth suggesting. I recommend floating the idea on the cpplang slack (there are #ranges and #future_standard channels) and/or the std-proposals mailing list. I never wrote a proposal, but you can chat with people who did. https://cpplang.slack.com/ https://cpplang.slack.com/ You can get an invitation here: https://cpplang-inviter.cppalliance.org/ https://cpplang-inviter.cppalliance.org/ https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals