我叫这个

bool cmpNAME(const PlayerCard& a, const PlayerCard& b){
    return (a.Name < b.Name);//Name is std::string
}
list<PlayerCard> tmp;
//fill tmp
stable_sort(tmp.begin(), tmp.end(), cmpNAME);//error at this line

和编译器给我的错误是它缺少PlayerCard运算符,但是为什么当我为此结构定义比较器时

我得到的错误:/usr/include/c++/4.8/bits/stl_algo.h|3508|error: no match for ‘operator-’ (operand types are ‘std::_List_iterator<PlayerCard>’ and ‘std::_List_iterator<PlayerCard>’)|
我要实现的是比较名称,如果名称相同,则按我将其推入列表的顺序进行比较

最佳答案

std::stable_sort需要一个随机访问迭代器。 list的迭代器不是随机访问的。

使用 std::list::sort

关于c++ - stable_sort缺少运算符-定义了自定义比较功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30014201/

10-11 22:40
查看更多