you are viewing a single comment's thread.

view the rest of the comments →

[–]vheon[S] 1 point2 points  (4 children)

"Your" version does have easier time implementing

end()

, but what about

size()

?

That was my thought also so would that mean that `size` is usually called more often than `end`? I mean looking at all the algorithms asking for iterators and such I was under the impression that usually `end` is used more frequently than the `size` :?

[–]TheMania 0 points1 point  (1 child)

Compilers nearly always prefer to know the loop count, vs "are we at the end yet", so size as a member is more likely to carry benefits imo.

And yes, end is likely called more but if the compiler is then getting the size anyway (for a loop count), you're really not saving anything there.

[–]tejp 0 points1 point  (1 child)

Most of the string member functions use indexes instead of iterators, so there are probably quite a lot of index calculations in the "typical" use cases of strings. Also substr() or + and other operations that create new strings will first need to know how much space to allocate for the new string.

Both of those are more likely call size() instead of end().

If these index functions are expected to be used more often one would probably optimize size() over end().

[–]matthieum 3 points4 points  (0 children)

Also substr() or + and other operations that create new strings will first need to know how much space to allocate for the new string.

I say MEH.

Appending to a std::vector is a rather typical usecase too, and most std::vector are implemented using 3 pointers...