Using STL algorithms, is it better to pass a function pointer or a functor?

前端 未结 6 2171
臣服心动
臣服心动 2021-02-20 05:04

Which of these 2 methods is better and why?

Method 1:

void fun(int i) {
  //do stuff
}

...
for_each(a.begin(), a.end(), fun);

Method 2

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 05:21

    #1 is simpler to declare the function
    while #2 the functor looks more like a function call.

    (Sometime you have to despair of c++ syntax)

提交回复
热议问题