4 ms·
I literally started programming in C++ this week and I figured the over-use of std::string couldn't be a good thing hehe
by vegancap 12y ago
I literally started programming in C++ this week and I figured the over-use of std::string couldn't be a good thing hehe
- csl 12y agoIt always depends on your needs. Before you start avoiding std::string, note that the article said that one of the problems was that they went from std::string to C-strings and then allocated a new std::string. This often happens when you use both kind of strings in your code. The takeaway is to stick either to C strings or use std::strings with reference passing, dropping down to c_str() when needed.
- vegancap 12y agoThanks, that was a lot more helpful response :) I think the most confused I've been so far (coming from a PHP background) was all the different types of strings!
- w8rbt 12y agostd::string solves more problems than it creates. Use it.
- zl4000 12y agoDefinitely, and where `std::string`'s interface is not enough, use thrid party libs which work on std::strings, like boost string algoriths, std regex, or uftcpp.
- pjmlp 12y agoWell, you could just use char* and produce security exploits everywhere.
- zl4000 12y agoNo that's BS. `std::string` should be used where ever it is applicable. The whole issue that this post about chrome was talking about was dealing with a poor usage of `std::string`, such as passing c_str() to then go and construct another string instead of passing by const ref. Or building a set of `std::string` to simply check if a value exists. That's just shit code, not an issue with `std::string`.
- vegancap 12y agoRight, I guess I misunderstood the article. As I said, new to C++.
- humanrebar 12y agoThere's a right way to do it. It's not the obvious way. Berating people for coming to terms with that isn't helpful. It's not their fault that C++ only really supports std::string out of the box. What are the alternatives? 1. const std::string & : what if I have a vector<char>? 2. const char * : what if it's not null terminated? 3. const char * and size_t : Better, but what if I have a deque<char>? 4. const char * start, const char * stop : Better because you can write algorithms around this, but still, doesn't help with deque<char>? 5. template on START_ITER and STOP_ITER : The best we have now if you need an extremely general solution. But I hope writing your implementation in headers is fine. 6. home grown type : a very popular choice, but http://xkcd.com/927/ http://xkcd.com/927/ 7. boost::string_ref : maybe the best choice, as it can be created from 1-4 (and most 6's), but still doesn't work with deque<char>. ...so give the rookie a break. But I'll support any comment in a code review about not accepting std::string by reference or by value in an interface.
- nightcracker 12y ago#5 and many, many other reasons is why C++ desperately needs a standardized range type. Let's pray it comes in C++17.
- pja 12y agoWhat would a range type offer over a pair of iterators?
- humanrebar 12y agocout << takeFirstN(sort(myVector), 10) << '\n'; ...try that with iterators.