我不知道如何在C++中正确实现列表的排序。我收到构建错误:



如何正确实现列表的排序功能?

Ranker.h:

list<Competitor*> sorted;

Ranker.cpp:
bool Ranker::compare(Competitor* first, Competitor* second) {
    if (first->getTime() < second->getTime())
        return true;
    else
        return false;
}

int Ranker::addList(Competitor* starter) {
    sorted.push_back(starter);
    sorted.sort(compare);
}

最佳答案

只需将Rankor设为一个简单的函子即可:

struct Compare
{
    bool operator()(Competitor* first, Competitor* second) const {
        return first->getTime() < second->getTime();
    }
};

// STUFF
sorted.sort(Compare());

或者如果您有C++ 11
sorted.sort([](Competitor* first, Competitor* second) {
          return first->getTime() < second->getTime();
           });

10-06 12:14