Possible Duplicate:
What is the difference between using a struct with two fields and a pair?
I been looking through a c++ quick reference app and noticed the UTILITY data structure pair< TypeName, TypeName>
to store a pair of variables in what i would think a struct. I'm confused on why this would be implemented as opposed to just using a simple struct, could someone enlighten me on why pair might be of any use?
pair<string, int> a("hello",3); // A 2-element struct
a.first; //"hello"
a.second; //3
I would think using a struct would be a better practice, as you can look back on the struct type and create objects of that specific struct while pair
could be prone to simple mistakes if multiple copies were needed.
struct Data
{
string str;
int num;
}
Data pair1;
pair1.str = "hello";
pair1.num = 3;