string::size_type instead of int

A short holds numbers too. As does a signed char. But none of those types are guaranteed to be large enough to represent the sizes of any strings. string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is. For … Read more

size_t vs int in C++ and/or C

In general, size_t should be used whenever you are measuring the size of something. It is really strange that size_t is only required to represent between 0 and SIZE_MAX bytes and SIZE_MAX is only required to be 65,535… The other interesting constraints from the C++ and C Standards are: the return type of sizeof() is … Read more

C++ for-loop – size_type vs. size_t

The C++ Standard says, size_type | unsigned integral type | a type that can represent the size of the largest object in the allocation model Then it adds, Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table … Read more

‘size_t’ vs ‘container::size_type’

The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same. However, if you use a custom allocator a different underlying type could be used. So container::size_type is preferable … Read more