在GCC中,std :: list的size()方法是O(n)。爲什麼?在GCC中,std :: list的size()方法是O(n)。爲什麼?
在標準C++ 11說大小(名單)應該是O(1) http://en.cppreference.com/w/cpp/container/list/size
然而,在我們的glibc有以下:
/usr/include/c++/4.6.3/bits/stl_list.h
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class list : protected _List_base<_Tp, _Alloc>
{
...
size_type
size() const
{ return std::distance(begin(), end()); }
的問題是:如何GCC中尚未實施三年前的要求嗎?
編輯:海灣合作委員會5改變了這一點:雖然在ABI變化的代價;這意味着使用gcc 5.0編譯的C++代碼將不適用於舊版本的C++運行時庫。
從 https://gcc.gnu.org/gcc-5/changes.html「的std ::列表的新的實現是默認啓用,用O(1)尺寸()函數」
g ++ 4.5 is from 2010.獲取最新版本! – 2014-10-27 03:31:50
很不錯,在4.6.3中它也是一樣的東西 – MichaelMoser 2014-10-27 03:39:41
在4.8.3中也是一樣的! – Galik 2014-10-27 04:00:45