jmasterx
jmasterx

Reputation: 54103

What is wrong with my Operator <?

I get an assertion failure about my Vec2's Operator <, I have no idea what is wrong though.

bool Vec2::operator<( const Vec2& v ) const
{
    if(x < v.x)
        return true;
    else
        return y < v.y;
}

Invalid Operator < for std set insert

template<class _Pr, class _Ty1, class _Ty2> inline
    bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, const _Ty1& _Left, const _Ty2& _Right,
        const wchar_t *_Where, unsigned int _Line)
    {   // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    if (!_Pred(_Left, _Right))
        return (false);
    else if (_Pred(_Right, _Left))
        _DEBUG_ERROR2("invalid operator<", _Where, _Line);
    return (true);
    }

Thanks

Upvotes: 1

Views: 1551

Answers (4)

Jarod42
Jarod42

Reputation: 217065

operator < should satisfy the Strict weak ordering.

A short way to do the job:

bool Vec2::operator< (const Vec2& v) const
{
    return std::tie(x, y) < std::tie(v.x, v.y);
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490018

Generally for a comparison like this, you want to compare the first pair of items, then if and only if those are equal, compare the second pair (and so on).

if (x < v.x)
    return true;
if (x > v.x)
    return false;
return y < v.y;

Upvotes: 0

YvesgereY
YvesgereY

Reputation: 3888

The corrected way to satisfy the ordering is :

bool Vec2::operator<( const Vec2& v ) const
{
    if(x < v.x)
        return true;
    if(x > v.x)
        return false;
    else        
        return y < v.y;
}

Or (code golf mode) :

bool Vec2::operator<( const Vec2& v ) const
{
    return (x != v.x)? (x < v.x)
                     : (y < v.y) ;
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

The problem is that this operator does not satisfy the weak ordering. For example consider two points

( 2, 1 ) and ( 1, 2 )

( 2, 1 ) is less ( 1, 2 ) because the second value 1 is less than 2.

At the same time ( 1, 2 ) is also less than ( 2, 1 ) because the first value 1 is less than the first value 2.

Look how thsi operator is defined for standard class std::pair and use the same operator.

Upvotes: 5

Related Questions