对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t? [英] Is std::container::size_type guaranteed to be size_t for standard containers with default allocator?

查看:150
本文介绍了对于具有默认分配器的标准容器,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
  • std::string<T>::size_type
  • std::list<T>::size_type
  • std::map<T>::size_type
  • std::vector<T>::size_type
  • etc.

cplusplus.com

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_stringsize_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆