From http://www.cplusplus.com/reference/utility/pair/, we know that std::pair
has two member variables, first
and second
.
Why did
The reason is that no real invariant needs to be imposed on the data structure, as std::pair
models a general-purpose container for two elements. In other words, an object of type std::pair
is assumed to be valid for any possible first
and second
element of type T
and U
, respectively. Similarly, subsequent mutations in the value of its elements cannot really affect the validity of the std::pair
per se.
Alex Stepanov (the author of the STL) explicitly presents this general design principle during his course Efficient Programming with Components, when commenting on the singleton
container (i.e., a container of one element).
Thus, albeit the principle in itself can be a source of debate, this is the reason behind the shape of std::pair
.