What exactly you want to use is a matter of how "purist" you want your code to be.
If you're on C++11, you can just use auto
and be done with.
Otherwise, in extremely generic code (which is designed to work with arbitrary allocators), you can use the container's nested typedef size_type
. That is taken verbatim from the container's allocator.
In normal use of standard library containers, you can use std::size_t
. That is the size_type
used by the default allocators, and is the type guaranteed to be able to store any object size.
I wouldn't recommend using [unsigned] int
, as that will likely be smaller than necessary on 64-bit platforms (it's usually left at 32 bits, although this of course depends on compiler and settings). I've actually seen production code fail due to unsigned int
not being enough to index a container.