What is std::pair
for, why would I use it, and what benefits does boost::compressed_pair
bring?
It's standard class for storing a pair of values. It's returned/used by some standard functions, like std::map::insert
.
boost::compressed_pair
claims to be more efficient: see here
std::pair comes in handy for a couple of the other container classes in the STL.
For example:
std::map<>
std::multimap<>
Both store std::pairs of keys and values.
When using the map and multimap, you often access the elements using a pointer to a pair.
Additional info: boost::compressed_pair is useful when one of the pair's types is an empty struct. This is often used in template metaprogramming when the pair's types are programmatically inferred from other types. At then end, you usually have some form of "empty struct".
I would prefer std::pair for any "normal" use, unless you are into heavy template metaprogramming.
What is std::pair for, why would I use it?
It is just as simple two elements tuple. It was defined in first version of STL in times when compilers were not widely supporting templates and metaprogramming techniques which would be required to implement more sophisticated type of tuple like Boost.Tuple.
It is useful in many situations. std::pair
is used in standard associative containers. It can be used as a simple form of range std::pair<iterator, iterator>
- so one may define algorithms accepting single object representing range instead of two iterators separately.
(It is a useful alternative in many situations.)