Re:In C++, is empty() faster than comparing the size with zero?
by plorkyeran on Thursday October 28, 2021 @02:04PM
(#29029120)
The standard's requirements for the standard library types don't imply any corresponding requirement for types you define yourself. If you want to write a container which can't reasonably do O(1) size() then you just don't do O(1) size(). If such a container was added to the standard library, the standard library's Container requirements will be adjusted to permit that.
[ Parent
| on HN ]
Re:In C++, is empty() faster than comparing the size with zero?
by chakkepolja on Thursday October 28, 2021 @02:43PM
(#29029808)
Exactly, most STL algorithms, for example, can be applied using iterators as well. But anyway if GP has specific requirements iterator invalidation might be a problem too, they wouldn't probably want to constrain their interface to that of STL containers.
Re:In C++, is empty() faster than comparing the size with zero?
by tialaramex on Thursday October 28, 2021 @04:42PM
(#29031671)
Wait, isn't the point of the STL algorithms that they use iterators?
For sure the iterator over a vector may be very cheap to use, but it is still an iterator, even if you tacitly know it's just pointer arithmetic under the hood.