Re: std::pair<,>

From: Neil Zanella (nzanella_at_cs.mun.ca)
Date: 10/04/03


Date: 4 Oct 2003 02:48:31 -0700


"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message

> The thing is
>
> coord.first = 2;
>
> is not quite representative, as opposed to
>
> coord.x = 2;
>
> Jonathan

That was exactly my point. As other posters have pointed out, there is a
function called std::make_pair(,) but that does not improve things much. Rather,
the main purpose of std::pair in the STL is so that in an associative container
such as an std::map or std::multimap you can use the .first notation to access
the key stored for a particular value (also accessed with .second). This is
quite handy since if you're iterating over a map you will need .first
to access the keys, and .second to access the values.

So, in conclusion, when dealing with points in two dimensional space (or other
dimensions for that matter) it's not very nice to use std::pair: that's not
what it was meant to. I don't think std::pair supports arithmetical operations
on points, and besides, using first and second instead of something like x and
y leads to poor naming. The names first and second are not suitable for all
applications from a semantical point of view: they make things harder to
interpret. The real use of std::pair is for providing first and second
to std::map and std::multimap.

Regards,

Neil