It depends on the implementation of list. list::empty() is guaranteed to be a constant time check, where list::size() can be linear time. In practice most implementations of list are such that both are constant time operations since the size is cached.
The reason an implementation might choose to have list::size() be a linear time operation would be so list::splice() is a constant time operation rather than having to count how many nodes were spliced to update the count. The only implementations I know of that did it that way for that specific reason are the sgi / STLPort variations which we don't use.
For all standard library containers it is considered more correct to check for empty() rather than size() == 0, but I probably wouldn't refactor working code to change just that unless it was to fix an actual performance problem versus a theoretical one.
|