c++ - 列表根本没有排序

标签 c++ visual-c++

我正在更新一个列表,然后尝试对列表进行排序,将最高的 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);
    }
};

谁能发现问题所在吗?

编辑:更新了代码,其中包含拼写错误...

最佳答案

这真是一团糟。

首先,您不是从 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 年历史的编译器,Microsoft 多年前就终止了对该编译器的所有支持。更重要的是,按照今天的标准,它是一堆散发着恶臭的垃圾。当它是新的时候它甚至不是很好。您需要退出 VS 6.0。

关于c++ - 列表根本没有排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537563/

相关文章:

c++ - 我将如何正确存储多个实例对象?

c++ - 双链表类上的迭代器

c++ - 错误:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}'

sql-server - 如何将 SQL_NUMERIC_STRUCT 转换为 double 和字符串?

c++ - 如何在控制台应用程序中定位提示?

c++ - `__declspec(novtable)`没有用吗?

C++ lambda 语法

c++ - 指向重载、模板化成员函数的 const 指针的静态数组

C++ 对象被向上转换为基类;不能调用派生方法

c++ - Visual Studio 2010 设置