What is the best practice for declaring a new variable for comparison with the size of a vector? which of the following should we favor (i.e. vector of doubles)?
uint compareVar;
std::uint64_t compareVar;
std::size_t compareVar;
std::vector<double>::size_type compareVar; // how is this different than size_t?
and why?
The one that you must use is std::vector::size_type
. it is usually std::size_t
but standard doesn't specify it. it is possible one implementation uses another type. You should use it whenever you want to refer to std::vector
size.
uint
, uint64_t
and even size_t
must not be used because in every implementation and platform the underlying vector implementation may use different types.
If you need an equality or ordinal comparison only, the best type would be the one used by vector's implementation, meaning
std::vector<MyType>::size_type compareVar
This guarantees that the type you use matches the type of vector's implementation regardless of the platform.
Note that since the type is unsigned, computing the difference between vector's size and compareVar
would require some caution to avoid incorrect results when subtracting a larger value from a smaller one.
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