4 ms·
Why would that be more difficult in C++?
by CyberDildonics 6y ago
Why would that be more difficult in C++?
- mehrdadn 6y agoBecause it doesn't even exist as provided functionality; you have to code basic stuff like this yourself. See for example https://stackoverflow.com/a/14267455 https://stackoverflow.com/a/14267455
- kenniskrag 6y agoIt is possible with a regex iterator #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; }
- mraza007 6y agoHey just curious i have only coded c++ in school I’m curious to know why don’t we use `using namespace std;` in real world when writing c++
- saagarjha 6y agoBecause it imports literally everything into your current scope, and the standard library types are named in such a way that they will likely conflict with code you write. Named a variable “min” or “max”? Function called “list”? You’re going to cause problems for yourself. 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.
- jdrek1 6y agoBecause it imports everything from the std namespace and can lead to name clashes. It also makes it harder to see where symbols come from. If you see `std::vector` you know exactly what it is. If you just see `vector` in a large code base it could be anything. Could be a hand-rolled version of std::vector from prehistoric times or people not trusting the STL for no reason. Could also be a mathematical vector or something for vector graphics or whatever else. That is why people generally always fully specify where things come from, it's not limited to just the STL. `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.
- mehrdadn 6y agoIn addition to the other comments (which are the primary reasons), beware that you can actually get different behavior due to something called "argument-dependent lookup". The most well-known example being std::swap(a, b). In general it is not equivalent to using namespace std; swap(a, b), though in this case, you actually want to say something else (using std::swap; swap(a, b)) to avoid importing the entire std namespace while also ensuring you call the ADL version of swap that may have been provided.
- secondcoming 6y agoStop
- kingaillas 6y agoThis is my annoyance with C++, it feels like the effort put into the language is disproportionate. As in, the committee is off solving problems that a handful of library implementors will use, but basic things like splitting strings or trimming whitespace is ignored. Yes these are fairly trivial but you either roll them over and over or start carrying around a little utility library.
- 0-_-0 6y agoAfter having used both Python and C++ for a while I concluded that most of the problems of C++ could be solved by a proper standard library.
- jcelerier 6y agoBeh, it's one function call if you use Qt or boost
- valbaca 6y agoDifficult, probably not. 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.
- seppel 6y agoIt 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?