Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Converting to string is the worst solution. Not smart, not fast, not lightweight
small string optimisation means that this should (probably?) never allocate on the heap.
I dont think this answer is best practice, but this comment thread starter is about as mislead as they thought the answer was.
I'm not sure about it... It's an example of how one usually should not do it. Alternatives are simple enough and I don't really like using strings for maths. At the same time it solves the given problem in the allocated time which is the ultimate goal. From this point of view it doesn't look that bad.
And by the way I'm not even sure if it uses heap at all: the size of
string
object itself is enough to store quite a few characters in place of pointer, capacity and length and modern implementations really do that.std::string is heap allocated, so std::to_string makes allocations .
converting things to strings is a relatively heavyweight operation as well... converting a number to a string to check a numeric property is a code smell.
its very bad, even for a junior or lacking experience... the sort of thing you might send around the office as a joke (i've seen exactly this with stack overflow answers using this class of approach)
Where's exactly allocation from the heap going?
I am totally agreed with semiessessi. It's a shame to call this piece of **** best practice.
... at the risk of being rude, this is a /worst/ practice solution. it allocates memory from the heap for a start. :(
upvoters should feel some shame for speaking without knowing what they are talking about.