如何用lambda进行排序? [英] How to sort with a lambda?

查看:148
本文介绍了如何用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:


错误C2564:'const char *':函数式转换为内置type只能有一个参数

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)

推荐答案

b

sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [](const MyClass & a, const MyClass & b) -> bool
{ 
    return a.mProperty > b.mProperty; 
});

我认为>操作符返回一个bool(每个文档)。但显然不是这样。

I assumed it'd figure out that the > operator returned a bool (per documentation). But apparently it is not so.

这篇关于如何用lambda进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆