In C++11 it is required that for any standard container the .size()
operation must be complete in “constant” complexity (O(1)). (Table 96 — Container requirements). Previously in C++03 .size()
should have constant complexity, but is not required (see Is std::string size() a O(1) operation?).
The change in standard is introduced by n2923: Specifying the complexity of size() (Revision 1).
However, the implementation of .size()
in libstdc++ still uses an O(N) algorithm in gcc up to 4.8:
/** Returns the number of elements in the %list. */
size_type
size() const _GLIBCXX_NOEXCEPT
{ return std::distance(begin(), end()); }
See also Why is std::list bigger on c++11? for detail why it is kept this way.
Update: std::list::size()
is properly O(1) when using gcc 5.0 in C++11 mode (or above).
By the way, the .size()
in libc++ is correctly O(1):
_LIBCPP_INLINE_VISIBILITY
size_type size() const _NOEXCEPT {return base::__sz();}
...
__compressed_pair<size_type, __node_allocator> __size_alloc_;
_LIBCPP_INLINE_VISIBILITY
const size_type& __sz() const _NOEXCEPT
{return __size_alloc_.first();}