0

我对 std::less 有一个奇怪的问题。

indexedpriorityq.hpp(21): error C2661: 'std::less<_Ty>::less' : no overloaded function takes 2 arguments
1>          with
1>          [
1>              _Ty=float
1>          ]

但这不就是它应该做的吗?

这是我的一些代码:

template<class KeyType, class binary_predicate = std::less<KeyType> >
class IndexedPriorityQ
{
 private:
    typedef typename std::vector<KeyType> KEYLIST;
    KEYLIST& m_Keys_V;

    [...]
};

template<class KeyType, class binary_predicate>
void IndexedPriorityQ<KeyType, binary_predicate>::
    ReorderUpwards(int size)
{
    while( (size>1) && 
        (binary_predicate(m_Keys_V[m_Heap_V[size]], m_Keys_V[m_Heap_V[size/2]])) //breaks here
         )
    {
        Swap(size/2, size);
        size /= 2;
    }
}

究竟是什么导致了错误,我该如何解决?

4

1 回答 1

2

std::less是一个仿函数,它的构造函数接受 0 个参数。也就是说,您可以像这样创建对象:

std::less<Key> a;

然后,您可以像这样使用它:

if(a(x,y)) ...

甚至

if(std::less<Key>()(x,y)) ...

有些函子的构造函数接受超过 0 个参数,例如std::bind1st. 规则是,如果函子是二进制的,则它operator()接受 2 个参数。

于 2012-04-21T15:42:47.517 回答