std::tuple vs std::array as items of a std::vector
For this specific case, I'd have to disagree with the comments. For homogeneous type containers - as is the case here (all int
s) - array
is superior.
When looking at the interface of std::tuple
vs. std::array
, it is very clear that the latter is a container (with iterators, e.g.), while the former is not. This means that the rest of the standard library will be much more naturally applicable to the latter.
If the types weren't homogeneous, there wouldn't be a question - it would have to be std::tuple
.
This depends a lot on the use case, but if the elements are somehow related, I would choose array
. You can iterate over array and use std algorithms with them.
I usually think tuple
as a substitute to something you could replace with a struct
like:
struct fourIntegers{
int int1;
int int2;
int int3;
int int4;
};
Sometimes the tuple
is just more compact/clear than a new struct
.