jma*_*erx 1 c++ operator-overloading operators
我得到关于我的Vec2操作员的断言失败<,我不知道有什么问题.
bool Vec2::operator<( const Vec2& v ) const
{
if(x < v.x)
return true;
else
return y < v.y;
}
Run Code Online (Sandbox Code Playgroud)
无效的运算符<用于标准集插入
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);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
问题是该运算符不满足弱排序.例如,考虑两点
(2,1)和(1,2)
(2,1)小于(1,2)因为第二值1小于2.
同时,(1,2)也小于(2,1),因为第一值1小于第一值2.
看看如何为标准类std :: pair定义thsi运算符并使用相同的运算符.