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

查看:29
本文介绍了如何用 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 排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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