So yeah, both are same; the only difference is that C++ defines size_t in std namespace.
size_type is a (static) member type of the type vector<int> . Usually, it is a typedef for std::size_t , which itself is usually a typedef for unsigned int or unsigned long long .
Yes, size_t is guaranteed to be an unsigned type.
Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three. Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes.
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 for maximum generality.
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.All std
containers use size_t
as the size_type
, but each independent library vendor chooses a type that it finds appropriate for its container.
If you look at qt, you'll find that the size_type
of Qt containers is version dependent. In Qt3 it was unsigned int
and in Qt4 it was changed to int
.
For std::[w]string
, std::[w]string::size_type
is equal to std::allocator<T>::size_type
, which is equal to the std::size_t
. For other containers, it's some implementation defined unsigned integer type.
Sometimes it's useful to have the exact type, so for example one knows where the type wraps around to (like, to UINT_MAX
) so that one can make use of that. Or for templates, where you really need to pass two identical types to function/class templates.
Often i find i use size_t
for brevity or iterators anyway. In generic code, since you generally don't know with what container instance your template is used and what size those containers have, you will have to use the Container::size_type
typedef if you need to store the containers size.
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