Hacker News new | ask | show | jobs
by dataflow 767 days ago
The most interesting thing imo is what they all do similarly: they all store the size, instead of the end pointer -- unlike, say, std::vector. Exercise for the reader as to why this is the right tradeoff.
1 comments

Maybe the sheer amount that strings are getting iterated through makes storing length computationally cheaper? Unless you're using very optimized methods to work with the string, the majority of string operations involve a lot of iteration and comparison. Vectors, on the other hand, have much more varied use and possibly may never be iterated at all (though they likely will somewhere).
> getting iterated through

So for loop conditions like

  for (size_t i = 0; i < str.size(); i++)
are cheaper to calculate?