#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s = "Python*C++*Java";
std::regex regex("\\*");
std::vector<std::string> out(std::sregex_token_iterator(s.begin(), s.end(), regex, -1), std::sregex_token_iterator());
for (auto &s: out) {
std::cout << s << '\n';
}
return 0;
}Plus, using std:: is generally a good reminder that you’re using the standard library types and not some homegrown facsimile of that type, as is common in many C++ projects.
`using namespace X;` is somewhat fine if you limit it to a scope by using it in a source file and ideally only the function body where you use it, in header files it is a really bad idea because then the namespace gets exposed to everything including that header. That can lead to the compiler calling the wrong function and you might not even notice it.
There is, however, boost::split from the famous boost library.
auto splitText = text | view::split(' ') | ranges::to<std::vector<std::<wbr />string>>();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.
You can get an invitation here: https://cpplang-inviter.cppalliance.org/
Verbose and clunky and needs a web search to do, yes.
I'm guessing it's analogous to Java's Regex. Like, yes, they're there and do as much as any other language, but lord it's a PITA.