什么是C++ 11中的lambda表达式?我什么时候用?他们解决了哪些问题在引入之前是不可能的?
一些示例和用例将是有用的.
我对C++ 11 lambdas遇到的一些例子感到困惑.例如:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << []()->string{return "Hello World 1!";}() << endl;
[]{cout << "Hello World 2!" << endl;}();
string result = [](const string& str)->string {return "Hello World " + str;}("2!");
cout << "Result: " << result << endl;
result = [](const string& str){return "Hello World " + str;}("3!");
cout << "Result: " << result << endl;
string s;
[&s](){s = "Hello World 4!";}; // does not work
cout << s …Run Code Online (Sandbox Code Playgroud)