Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair?

like image 257
Tamara Wijsman Avatar asked Feb 10 '10 10:02

Tamara Wijsman


People also ask

Is pair a struct?

A std::pair is a struct.

What is struct pair in C++?

Pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples. The pair container is a simple container defined in <utility> header consisting of two data elements or objects.

What is the difference between struct and struct?

Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.

What is std :: pair?

std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.


1 Answers

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.

like image 105
AshleysBrain Avatar answered Oct 26 '22 12:10

AshleysBrain