-1

Initially I had code that looked like this:

    std::map< std::pair<int,int>, std::vector<Class0*> > aMap;

It worked. Now I have code that looks like this:

    std::map< std::pair<Vec3f, Vec3f>, std::vector<Class0*> > aMap;

It no longer maps correctly (compiles fine). Why? And how can I fix that?

EDIT: After popular demand here is the comparison code for a 3D vector (3 floats):

    class Vec3f {
        ...

        bool operator () ( const Vector3f& v0, const Vector3f& v1 ) const {

                return std::tie(v0[0], v0[1], v0[2]) < std::tie(v1[0], v1[1], v1[2]);

    }       ...

from this question Overloading operator for set. The above comparison works fine for a set, but apparently not for a pair. Why?

Community
  • 1
  • 1
Tring Vu
  • 253
  • 1
  • 10
  • 1
    *"It no longer maps correctly (compiles fine)"* Could you please elaborate? What's the expected and observed behaviour? Can you provide a [MCVE](http://stackoverflow.com/help/mcve)? – dyp Dec 25 '14 at 00:41
  • Does `Class1` has correct copy constructor ? – Jarod42 Dec 25 '14 at 00:42
  • A little more detail would be helpful, such as,e.g. the definition of class1, your comparison operator and what exactly you mean with "It no longer maps correctly". – MikeMB Dec 25 '14 at 00:47
  • _"Class 1 already has correct comparison operators."_ A [MCVE](http://stackoverflow.com/help/mcve) for `Class1` and `Class0` would be much appreciated! – πάντα ῥεῖ Dec 25 '14 at 00:47
  • @MikeMB, I ran into this issue earlier with std::set. Class1 is the same as the one described here http://stackoverflow.com/questions/27030756/overloading-operator-for-set. Would the comparison operator work for a std::set but not a std::pair? – Tring Vu Dec 25 '14 at 00:49
  • 1
    @TringVu Make that clear in your actual question, instead of linking to another one. Don't even think about editing that link into your question, provide the full code, error messages to reproduce the problem (unfortunately you were faster than me doing it actually). – πάντα ῥεῖ Dec 25 '14 at 00:51
  • How do you compute float for key ? maybe floating rounding error problem. – Jarod42 Dec 25 '14 at 00:54
  • 2
    You show us `operator ()`, not `operator <`... – Jarod42 Dec 25 '14 at 00:57
  • @Jarod42 I assumed operator() encompasses operator – Tring Vu Dec 25 '14 at 00:59
  • @TringVu _I assumed operator() encompasses `operator<`_ Huh, how? – πάντα ῥεῖ Dec 25 '14 at 01:07
  • @ πάντα ῥεῖ It doesn't, I have come to learn. I added operator< and operator> and now it works as expected. – Tring Vu Dec 25 '14 at 01:08
  • @TringVu So what do you think now? Is this question really helpful for someone researching for the same problem you actually have experienced? – πάντα ῥεῖ Dec 25 '14 at 01:11
  • 1
    --1 -- use of `...` in code. Making a MCVE was asked, and would be easy, but you chose not to. This means we have to determine if the problem is your transcription or the actual error, – Yakk - Adam Nevraumont Dec 25 '14 at 01:35

1 Answers1

6

The key to the map is a pair. The comparison of pair is lexicographic: it first compares the first elements of both pairs. If both appears to be equal, it also compares the second elements. So you need a proper comparison for both Class1 and Class.

Additional considerations:

On each class of the pair, the map comparator less-than must comply with some constraints:

  • it must establish a stritct weak oredering between all the elements.
  • (! k1<k2) && (! k2<k1) is equivalent to k1==k2
  • k1<k2 && k2<k3 implies that k1<k3

If any of this property is broken, the strict order is not guaranteed and the mapping may fail.

Christophe
  • 68,716
  • 7
  • 72
  • 138