我想知道当我们在循环中声明一个函数时会发生什么,比如x次运行.例如,
int main() {
for(int i=0;i<100;i++)
{
void my_func(){
cout<<"Hello! Brother"<<endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
正常功能无法实现您的功能.但是,您可以使用lambdas来实现所需的结果:
int main()
{
for (int i = 0; i < 100; i++)
{
// Create local lambda and call it afterwards.
auto myfunc = []() {
cout << "Hello! Brother" << endl;
};
myfunc();
// alternatively, call lambda in situ
// []() { cout << "Hello! Brother" << endl; }();
}
}
Run Code Online (Sandbox Code Playgroud)