lambda函数的用途是什么?
What is the use of lambda functions?
谁能帮我理解下面的代码
#include <iostream>
using namespace std;
int main()
{
auto hello = []() -> void {
cout << "Hello World";
};
// Call the lambda function
hello();
}
这里auto hello = []() -> void
有什么用?我不明白花括号(第7行)后面的终止分号的含义
把它当作一行来读:
auto hello = []() -> void { cout << "Hello World"; };
hello
是一个变量,它包含
- 不捕获外部变量(
[]
), - 不带参数(
()
), - 返回
void
(-> void
,这被称为'尾随返回类型',指定lambda的返回类型,就像您对普通函数一样。),和 - 由单个
cout
语句组成。
直到下一行显式调用它时才实际执行。
相关文章:
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 是什么让放置新调用对象的构造函数?
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 在函数中拥有函数原型的目的是什么?
- 使用基类指针调用基类的值构造函数的语法是什么?
- C++:使用方法调用析构函数的顺序是什么?
- 将此布尔值传递给此函数的最有效方法是什么?
- * 和 ** 在 C++ 函数声明中是什么意思?
- C++:Lambda 函数指针转换的用例是什么?
- C++关于指针和使用函数将它们启动到堆的行为究竟是什么?
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 在将函数声明为友元时,尖括号的含义是什么?
- 在模板类之外定义友元函数的正确方法是什么?
- 在使用包含冒号的类似函数的宏时,是什么导致了这种编译器差异?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 这里的字符串函数是什么意思
- 传递 lambda 函数的权衡是什么?
- 在C 中,对2D数组的增量是什么?函数断言(0)做什么?