#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.
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.
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/
Do you want to truncate the string? return new strings? return string views? It all depends what you need.
In some languages all strings are immutable and all operations result in new strings and you have no control over that.
Check the Boost C++ string algorithms library.
https://www.boost.org/doc/libs/1_74_0/doc/html/string_algo/u...
From memory the less horrible experience i had was with Go, but there's help for the slices on the runtime which let the difficult parts hidden and also the batteries-included std library, which is one of the most well designed standard libraries out there.
Buffer management is one of the things that sucks the most in any language, and the ones that dont its because the complexity is hidden in another fundamental layer.
scala> "Mary had a little lamb. The lamb was in federal witness protection.".split("lamb")
res0: Array[String] = Array("Mary had a little ", ". The ", " was in federal witness protection.")I meant for languages where the hard parts are delegated to another like Python and Javascript (C and C++ respectively).
"a, b".Split(", ") // C#
"a, b".split(", ") # Python "a, b".split(", ") split "foo.bar" .
=> foo barThis misses the point. I don't care how the language implements the functionality if it meets my performance requirements and it is convenient to use.
I dont think it does, because it meant you are working on a simple language that can only afford to be simple delegating the harder parts to other languages that took the effort to be designed to deal with everything.
The convenience is payed by someone else, and the language that pay for the hard stuff are the ones that are complex, because they need to work well with any sort of algorithms.
Unicode is a slight pain (it's explicitly supported and works fine but it was designed before UTF8 dominated so there's baggage)