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?
In terms of performance: it's unlikely to change anything, you're just sugar coating it.
In terms of usability, I would rather use a custom struct, which can be declared this way (by the way):
struct MyElement
{
int label;
double value;
};
I am a strong proponent of strong typing, and I much prefer a "real" structure (and better yet, class) than an ad-hoc tuple whenever it's more than a fleeting thing.
Mainly because:
first
and second
don't carry much meaningstd::pair
std::pair
All in all, I really think that maintenance benefits from using a custom dedicated structure that a one-size-fits-them-all tuple.