How do I pass a function as parameter?

后端 未结 3 1434
时光说笑
时光说笑 2020-12-21 22:22

What is the fastest and shortest way to pass a function as parameter of another function without using other libraries other than the std one in just one line?

I mea

3条回答
  •  情歌与酒
    2020-12-21 23:24

    C++11 provides anonymous functions:

    forloop(3, []{ std::cout "Hi!"; });
    

    Example:

    #include 
    #include 
    
     void forloop(int times, std::function f) {
         for(int i = 0; i < times; i++) {
             f();
         }
     }
    
    int main() {
        forloop(3, [] () { std::cout << "Hello world"; });
    }
    

提交回复
热议问题