What is the difference between using a struct with two fields and a pair?

std::pair provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator < (such as required by std::map). If you don't write them you can't make a mistake (remember how strict weak ordering works?) so it's more reliable just to use std::pair.


std::pair comes with a number of constructors and operators.

A struct allow named fields (other than first and second) and is ready to be extended at any time.

Prefer a struct when you can. It may involve some overhead, but is definitely easier for maintenance.


In terms of memory allocation and efficiency, there is no difference -- since that's exactly what a std::pair is.