Why doesn't std::pair have iterators?

Frank picture Frank · Dec 6, 2012 · Viewed 7.2k times · Source

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?

Answer

NPE picture NPE · Dec 6, 2012

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>.