场景

        定义函数如下:

template <class T> struct greaterkey {//用于指定map当中的排序,递减,map中默认递增
 bool operator() (const T& x, const T& y) const
 {
  return x > y;
 }
};
template <class T> struct lesskey {
 bool operator() (const T& x, const T& y) const
 {
  return x < y;
 }
};


解决

vs2010后都是严格比较,相等的两个元素,一定要返回false,可以看到STL的源码:
[cpp]
template<class _Pr, class _Ty1, class _Ty2> inline
    bool _Debug_lt_pred(_Pr _Pred,
        _Ty1& _Left, _Ty2& _Right,
        _Dbfile_t _File, _Dbline_t _Line)
    {   // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    if (!_Pred(_Left, _Right))
        return (false);
    <strong>else if (_Pred(_Right, _Left))
        _DEBUG_ERROR2("invalid operator<", _File, _Line);</strong>
    return (true);
    }
如果left和right都一样,STL就会报错。。所以只能修改下自己的代码,当相等的时候,就返回false了


template <class T> struct greaterkey {//用于指定map当中的排序,递减,map中默认递增
 bool operator() (const T& x, const T& y) const
 {

    if (x == y) return false;
      return x > y;
 }
};
template <class T> struct lesskey {
 bool operator() (const T& x, const T& y) const
 {

    if (x == y) return false;

      return x < y;
 }
};