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