Why doesn't std::pair
have iterators?
std::pair
should provide iterator
and const_iterator
as well as begin()
and end()
-- just for their two members.
I think it would be useful because then we could pass them into templated functions that expect iterables, like vector
or set
.
Are there any downsides to this?
One reason is that the two elements of a pair can be of different types. This doesn't fit with the iterator model.
The same goes for tuples, where having iterators would perhaps be even more appealing.
If you need an inexpensive homogenous container of a fixed length, you could use std::array<T, n>
.