2

我正在更新一个列表,然后尝试对列表进行排序,将最高sVar1的放在列表的前面,但它没有这样做,我使用的是 6.0 VS

while(Iter != m_SomeList.end())
{
    if((*Iter)->sVar1 == 1) 
    {
        (*Iter)->sVar1++;
    }

    Iter++;
}

m_SomeList.sort(Descending());

Iter = m_SomeList.begin();

while(Iter != m_SomeList.end())
    {
        //now display the content of the list

我的降序排序功能

struct Descending : public greater<_LIST_DETAIL*> 
{
    bool operator()(const _LIST_DETAIL* left, const _LIST_DETAIL* right)
    {
        return (left->sVar1 > right->sVar1);
    }
};

谁能发现什么问题?

编辑:更新的代码,它包含错别字...

4

1 回答 1

2

这简直是​​一团糟。

首先,您不会派生自std::greater——您要么直接使用 std::greater,如:

(伪代码,未编译)

std::sort( v.begin(), v.end(), std::greater() );

...或者,如果这对您没有好处,请通过以下方式编写您自己的函子std::unary_function

struct Descending : public unary_function<bool, _LIST_DETAIL>
{
    bool operator()(const _LIST_DETAIL* left, const _LIST_DETAIL* right) const // note const
    {
        return (left->sVar1 > right->sVar1);
    }
};

其次,语言保留使用前导下划线后跟大写名称。

第三,您使用的是 14 年前的编译器,微软多年前就终止了对它的所有支持。更重要的是,按照今天的标准,它是一堆发臭的垃圾。当它是新的时它甚至不是很好。你需要离开 VS 6.0。

于 2012-05-10T16:05:30.307 回答