0

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;
Community
  • 1
  • 1
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
  • 3
    Child reinvents wheel. Scientists baffled. – Kerrek SB Dec 17 '12 at 18:25
  • My apologies, i searched the forums for a question similar to this but obviously used the wrong keywords. – Syntactic Fructose Dec 17 '12 at 18:41
  • 1
    C++ does not have anything called an `UTILITY`, or for that matter, a `STRUCT`. It does have a thing called a `struct` (note, C++ is case sensitive). And it has a class template called `pair` (or, if you want to be explicit, `std::pair`), which exists in a header named `utiity` (again, case sensitive). You'll make everyone's lives much easier if you refer to things by their names. ;) – jalf Dec 17 '12 at 19:26

0 Answers0