Like:
std::string<T>::size_type
std::list<T>::size_type
std::map<T>::size_type
std::vector<T>::size_type
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?
size_t is defined as the type used for the size of an object and is platform dependent. container::size_type is the type that is used for the number of elements in the container and is container dependent.
std::size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.
The signed equivalent of size_t is ptrdiff_t , not int . But using int is still much better in most cases than size_t. ptrdiff_t is long on 32 and 64 bit systems. This means that you always have to convert to and from size_t whenever you interact with a std::containers, which not very beautiful.
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:
However, for basic_string
, size_type
is defined as
typedef typename allocator_traits<Allocator>::size_type size_type;
which in turn will be size_t
for std::allocator<..>
as the allocator.
Also, std::array
uses size_t
as size_type
, according to [array.overview]/3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With