对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t? [英] Is std::container::size_type guaranteed to be size_t for standard containers with default allocator?
问题描述
赞:
-
std::string<T>::size_type
-
std::list<T>::size_type
-
std::map<T>::size_type
-
std::vector<T>::size_type
- 等
std::string<T>::size_type
std::list<T>::size_type
std::map<T>::size_type
std::vector<T>::size_type
- etc.
Both cplusplus.com and cppreference.com say that they are usually size_t
, but are they truly, unambiguously guaranteed by the standard to be size_t
unless a custom allocator is used?
推荐答案
对于STL容器-不. [container.requirements.general]中标准的表96列出了任何容器X
的容器要求,很清楚地说明了这一点:
For STL-containers - nope. Table 96 of the standard in [container.requirements.general], which lists container requirements for any container X
, explains it pretty clear:
但是,对于basic_string
,size_type
被定义为
typedef typename allocator_traits<Allocator>::size_type size_type;
,对于std::allocator<..>
作为分配器,它将依次为size_t
.
which in turn will be size_t
for std::allocator<..>
as the allocator.
此外,根据[array.overview]/3,std::array
使用size_t
作为size_type
.
Also, std::array
uses size_t
as size_type
, according to [array.overview]/3.
这篇关于对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!