使用 std::sort 时“二进制表达式的操作数无效”

msh*_*dal 0 c++ sorting std

invalid operands to binary expression当我尝试编译使用 std::sort 的项目时,出现错误。

我正在使用 std::sort 像这样:

vector <record> vrec;
...
sort(vrec.begin(), vrec.end());
Run Code Online (Sandbox Code Playgroud)

我已经重载了 < 运算符,如下所示:

bool operator< (record &r1, record &r2) { ... }
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息的摘录:

invalid operands to binary expression ('const record' and 'const record')

operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}

                                                         ~~~ ^ ~~~
Run Code Online (Sandbox Code Playgroud)

Man*_*726 5

operator<必须按值(const 引用或复制)获取参数,而不是引用:

bool operator<( const record& lhs , const record& rhs ) 
{
    return /* whatever comparison criteria you have */;
}
Run Code Online (Sandbox Code Playgroud)