While debugging something, I saw the STL vector::empty() implementation:
bool empty() const
{return (size() == 0); }
I believe, whenever we are probing the emptiness of vector it is always recommended to use empty over size(). But seeing that implementation, I am wondering, what is the benefit of doing so? Instead, there is a function call overhead in calling empty as it internally calls size()==0.
I thought empty() may be helpful in case of list as size() doesn't guarantees the constant time in list. To verify my assumption, I checked the list implementation and surprisingly, found the same implementation in list as well,
return (size() == 0);
I am bit confused now. If empty internally uses size() then why should we prefer empty over size() ?
question from:
https://stackoverflow.com/questions/743197/size-vs-empty-in-vector-why-empty-is-preferred 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…