如何在 C++ 中定义匿名函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12483753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do define anonymous functions in C++?
提问by danijar
Can I define functions in C++ inline? I am not talking about lambda functions, not the inline
keyword that causes a compiler optimization.
我可以在 C++ 中内联定义函数吗?我不是在谈论 lambda 函数,也不inline
是导致编译器优化的关键字。
回答by Adam Rosenfield
C++11 added lambda functionsto the language. The previous versions of the language (C++98 and C++03), as well as all current versions of the C language (C89, C99, and C11) do not support this feature. The syntax looks like:
C++11 向语言添加了lambda 函数。该语言的先前版本(C++98 和 C++03)以及所有当前版本的 C 语言(C89、C99 和 C11)都不支持此功能。语法如下:
[capture](parameters)->return-type{body}
For example, to compute the sum of all of the elements in a vector:
例如,要计算向量中所有元素的总和:
std::vector<int> some_list;
int total = 0;
for (int i=0;i<5;i++) some_list.push_back(i);
std::for_each(begin(some_list), end(some_list), [&total](int x) {
total += x;
});
回答by Kerrek SB
In C++11, you can use closures:
在 C++11 中,你可以使用闭包:
void foo()
{
auto f = [](int a, int b) -> int { return a + b; };
auto n = f(1, 2);
}
Prior to that, you can use local classes:
在此之前,您可以使用本地类:
void bar()
{
struct LocalClass
{
int operator()(int a, int b) const { return a + b; }
} f;
int n = f(1, 2);
}
Both versions can be made to refer to ambient variables: In the local class, you can add a reference member and bind it in the constructor; and for the closure you can add a capture listto the lambda expression.
两个版本都可以引用环境变量:在本地类中,可以添加引用成员,并在构造函数中绑定;对于闭包,您可以向 lambda 表达式添加捕获列表。
回答by CyberGuy
i dont know if i understand you well, but you want a lambda function?
我不知道我是否理解你,但你想要一个 lambda 函数?
http://en.cppreference.com/w/cpp/language/lambda
http://en.cppreference.com/w/cpp/language/lambda
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
std::vector<int> c { 1,2,3,4,5,6,7 };
int x = 5;
c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
std::cout << "c: ";
for (auto i: c) {
std::cout << i << ' ';
}
std::cout << '\n';
std::function<int (int)> func = [](int i) { return i+4; };
std::cout << "func: " << func(6) << '\n';
}
if you dont have c++11x then try:
如果您没有 c++11x,请尝试:
回答by Thomas Eding
Pre C++11, if you want to localizea function to a function, that can be done:
在 C++11 之前,如果要将函数本地化为函数,可以这样做:
int foo () {
struct Local {
static int bar () {
return 1;
}
};
return Local::bar();
}
or if you want something more complicated:
或者如果你想要更复杂的东西:
int foo (int x) {
struct Local {
int & x;
Local (int & x) : x(x) {}
int bar (int y) {
return x * x + y;
}
};
return Local(x).bar(44);
}
But if you want a true function literal in pre C++11, that is not possible.
但是,如果您想要 C++11 之前的真正函数字面量,那是不可能的。