Possible Duplicate:
What is the difference between using a struct with two fields and a pair?
Dear all,
I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ?
I have used pairs for a while but the main problem is readability :
If you want to represent for example a duple (int “label”, double “value”) you can use either a :
typedef std::pair<int,double> myElem;
or a
typedef struct {
int label;
double value;
} myElem;
The code becomes more readable if your statements have a “semantic” sense (you will always know what x.label is. that’s not the case with x.first).
However, I guess there is an advantage using pair. Is it more performant or something else?
A
pairis implemented as a templatedstruct. It provides you with a shorthand for creating a (typically heterogenous) pair. Also, there are some constraints on the types that you can use with apair:(from SGI STL
std::pairdocumentation)Defining your own POD may make sense if the types do not follow any of these constraints or if you do not care about them.
Finally, I guess it is a matter of personal choice/coding style.