[capture-list] (parameters) -> return-type {
// 函数体
}
例子:
int x = 10;
auto function = [=](int a, int b) mutable -> int {
return a + b + x;
}
int ret = function(10, 20); // 输出50
#include
#include
void abssort(float* x, unsigned n) {
std::sort(x, x + n,
[](float a, float b) {
return (std::abs(a) < std::abs(b));
}
);
}
Lamdba表达式在QT中的应用
QTimer *timer=new QTimer;
timer->start(1000);
QObject::connect(timer, &QTimer::timeout, [&]() {
qDebug() << "time out";
});
int a = 10;
QString str1 = "Key press";
connect(pBtn4, &QPushButton::clicked, [=](bool checked) {
qDebug() << a <<str1;
qDebug() << checked;
});