我正在尝试按飞行员参加比赛的时间来排序指向飞行员对象的指针列表。时间是通过Pilot类中的Competition方法计算的。因此,我将通过方法而不是类属性对它们进行排序。
问题涉及的类:

class Pilot
{
    double compete(Race *race); // Computes the time the pilot takes to finish the race
}
class Race
{
    list<Pilot*> pilots; // Pilots competing

    void sortByTime();
}

我想使用lambda表达式对列表进行排序。这是我的尝试:
void Race::sortByTimes()
{
    sort(pilots.begin(), pilots.end(), [this](Pilot *p1, Pilot *p2) -> bool
    {
        return p1->compete(this) < p2->compete(this);
    });
}

我在算法库中遇到5个编译错误:
Error C2784 'unknown.type std::operator - (std::move_iterator<_RanIt> &, const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RantIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<PilotoOficial*>>>'

std::reverse_iterator和std::__ Revranit中还有两个C2784错误
Error C2676 binary : 'std::_List_iterator<std::_List_val<std::_List_simple_types<Pilot*>>>' : does not define this operator or a conversion to a type acceptable to the predefined operator

Error C2780 'void std::_Sort(_RanIt,_RanIt,_Diff,_Pr' : expects 4 arguments; 3 provided

最佳答案

使用 std::list::sort std::sort需要随机访问迭代器,并且您已将其指定为双向迭代器。另一种选择是使用像std::vector这样的具有随机访问迭代器的容器。

09-10 02:58
查看更多