c++ - stable_sort 缺少运算符 - 定义了自定义比较函数

标签 c++

我称之为

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

并且编译器给我错误,它缺少 operator- for 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/

相关文章:

c++ - 如何将整个 Visual c++ 项目加载到 Enterprise Architect 中以对其进行逆向工程?

c++ - Qt问题将参数传递给插槽

c++ - 模板推导中的const T&和T&有什么区别

c++ - 在 C++ 中实现进度可视化

c++ - 使用 Windows 7 在 VS 中取消引用 null

c++ - 实时音频,快速循环中的临时缓冲区,不同的方法

c++ - 打印 C++ 结构中所有字段的值

c++ - 返回值是类名后跟一对空括号是什么意思?

c++ - 为什么 "is_modulo"对于 double 和 float 是假的?

C++ 使用 "this"对象的 static_cast 和 const_cast 添加 const-ness 之间的区别?