像:對於具有默認分配器的標準容器,std :: container :: size_type保證爲size_t?
std::string<T>::size_type
std::list<T>::size_type
std::map<T>::size_type
std::vector<T>::size_type
- 等
兩個cplusplus.com和cppreference.com說,他們通常size_t
,但他們是否真的,明確保證由標準是size_t
,除非使用自定義分配器?
像:對於具有默認分配器的標準容器,std :: container :: size_type保證爲size_t?
std::string<T>::size_type
std::list<T>::size_type
std::map<T>::size_type
std::vector<T>::size_type
兩個cplusplus.com和cppreference.com說,他們通常size_t
,但他們是否真的,明確保證由標準是size_t
,除非使用自定義分配器?
對於STL容器 - 不。在[container.requirements.general]的標準,其中列出了爲任何容器X
容器要求的表96,解釋它非常清楚:
然而,對於basic_string
,size_type
被定義爲
typedef typename allocator_traits<Allocator>::size_type size_type;
然後將size_t
作爲std::allocator<..>
作爲分配器。
另外,根據[array.overview]/3,std::array
使用size_t
作爲size_type
。
size_type
不保證是size_t
。
但是default allocatorsize_type
是,所以默認是size_t
。
從標準20.6.9
template <class T> class allocator {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
....
容器的SIZE_TYPE從分配器導出:
typedef typename allocator_traits<Allocator>::size_type size_type;
這僅僅是'basic_string'如此。第23條容器不保證使用分配器的'size_type'。 – 2014-10-17 21:52:34