tags:

views:

176

answers:

4

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

+6  A: 

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.

AshleysBrain
I don't think std::pair provides any constructors or operators for its members. What exactly are you thinking of?
Manuel
j_random_hacker
Yes but the OP seemed to be implying that std::pair *magically* generated those members for the underlying types. It's weird that this answer is the one that got accepted.
Manuel
The wording could have been a little better but I do think AshleysBrain knows what (s)he's talking about.
sellibitze
Edited to try and clarify the wording.
AshleysBrain
+3  A: 

In terms of memory allocation and efficiency, there is no difference -- since that's exactly what a std::pair is.

j_random_hacker
That is what answers what I was wondering about, I suddenly realized that efficiency really depends on what you would do so is a bit too much information. I'm going to accept AshleysBrain's question for thinking further...
TomWij
No problem, but it would have made more sense to ask about comparing ease-of-use if that's what you're interested in. Naveen and I both answered the question as stated. (Not complaining, just saying...)
j_random_hacker
Hmm, seem to have made a serious typo in my comment. "That is the answer I was wondering about" it should have been. I gave AshleysBrain the accepted answer as he was was quick and gave a good answer, just like you. It was hard to choose, but I went for the one who would benefit most.
TomWij
+1  A: 

No difference in terms of memory allocation or efficiency. In fact, in the STL implementation I am using pair is defined as struct pair

Naveen
What STL implementation are you using ?
Benoît
I am using the one which comes with VC9 compiler.
Naveen
Yes, this adheres to the common convention that if a user-defined type exposes public data members (like `pair` does with `first` and `second`) then it has to be a `struct`.
Manuel
Could be a `class` with `first` and `second` declared in a `public` section. According to the interface, the `first` and `second` members must be public; doesn't state whether it must be a `class` or `struct`.
Thomas Matthews
@Thomas Matthews: There is no semantic difference whatsoever between `struct X { ... };` and `class X { public: ... };`. (Unlike in C# for example.)
j_random_hacker
+1  A: 

std::pair comes up with a number of constructors and operators.

A struct allow named fields (other than first and second) and is ready to be extended at any time.

Prefer a struct when you can, it may involve some overhead, but is definitely easier for maintenance.

Matthieu M.
+1 for the maintenance comment. I worked with a group of engineers from NJ who tried to do everything (and I mean everything) with the STL, and rather than creating appropriate classes/structs when the design indicated, they used an STL container. Code was littered with "if (route.first.second[*iter].first) { ... }". Ugh!
Don Wakefield
Good idea, will keep this in mind for later.
TomWij