How to sort with a lambda?(如何用 lambda 排序?)
问题描述
sort(mMyClassVector.begin(), mMyClassVector.end(),
[](const MyClass & a, const MyClass & b)
{
return a.mProperty > b.mProperty;
});
我想使用 lambda 函数对自定义类进行排序,而不是绑定实例方法.但是,上面的代码会产生错误:
I'd like to use a lambda function to sort custom classes in place of binding an instance method. However, the code above yields the error:
error C2564: 'const char *' : 向内置类型的函数式转换只能采用一个参数
error C2564: 'const char *' : a function-style conversion to a built-in type can only take one argument
与 boost::bind(&MyApp::myMethod, this, _1, _2) 配合使用效果很好.
It works fine with boost::bind(&MyApp::myMethod, this, _1, _2).
推荐答案
知道了.
sort(mMyClassVector.begin(), mMyClassVector.end(),
[](const MyClass & a, const MyClass & b) -> bool
{
return a.mProperty > b.mProperty;
});
我认为它会发现 > 运算符返回一个布尔值(根据文档).但显然并非如此.
I assumed it'd figure out that the > operator returned a bool (per documentation). But apparently it is not so.
这篇关于如何用 lambda 排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用 lambda 排序?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
