繁体   English   中英

如何使用不同的参数在C ++中多次调用函数

[英]How to call a function several times in C++ with different parameters

我有下一个代码:

object a,b,c;
fun (a);
fun (b);
fun (c);

我想知道是否有任何方法可以在C ++ 98或C ++ 11中做类似的事情来:

call_fun_with (fun, a, b, c);

谢谢

这是一个可变参数模板解决方案。

#include <iostream>

template < typename f_>
void fun( f_&& f ) {}

template < typename f_, typename head_, typename... args_>
void fun( f_ f, head_&& head, args_&&... args) {
    f( std::forward<head_>(head) );
    fun( std::forward<f_>(f), std::forward<args_>(args)... );
}

void foo( int v ) {
    std::cout << v << " ";
}

int main() {
  int a{1}, b{2}, c{3};
  fun(foo, a, b, c );
}

您可以使用variadic模板使用以下内容:

template <typename F, typename...Ts>
void fun(F f, Ts&&...args)
{
    int dummy[] = {0, (f(std::forward<Ts>(args)), 0)...};
    static_cast<void>(dummy); // remove warning for unused variable
}

或者在C ++ 17中,使用折叠表达式:

template <typename F, typename...Ts>
void fun(F&& f, Ts&&...args)
{
    (static_cast<void>(f(std::forward<Ts>(args))), ...);
}

现在,测试一下:

void foo(int value) { std::cout << value << " "; }

int main(int argc, char *argv[])
{
    fun(foo, 42, 53, 65);

    return 0;
}

使用C ++ 11,你可以用这种方式使用std::function (编写IMO非常快)

void call_fun_with(std::function<void(int)> fun, std::vector<int>& args){
    for(int& arg : args){
        fun(arg);
    }
}

或者,更通用一点:

template<typename FTYPE>
void call_fun_with(FTYPE fun, std::vector<int>& args){
    for(int& arg : args){
        fun(arg);
    }
}

实例

实例,模板版

注意:必须按以下方式指定std::function模板参数: return_type(arg1_type, arg2_type,etc.)

编辑 :一个替代方案可能是使用std::for_each实际上做了几乎相同的事情,但我不喜欢语义,这更像是“对于这个容器中的所有东西,做......”。 但那只是我和我(也许是愚蠢的)编码方式:)

有很多不同的方式......

#include <iostream>
#include <vector>
#include <algorithm>

void foo(int x) {
    std::cout << x << "\n";
}

void call_fun_with(std::function<void(int)> fn, std::vector<int> lst) {
    for(auto it : lst)
      fn(it);
}

int main() {
    std::vector<int> val = {1,2,3,4,5};

    // c++98
    std::for_each(val.begin(), val.end(), foo);

    // c++11
    // vector
    call_fun_with(foo, val);

    // c++11
    // initializer_list
    int a=0, b=1, c=2;
    call_fun_with(foo, {a,b,c});
}

看到这里

基于C ++ 11范围(增强)for循环将braced-init-list识别为initializer_list ,这意味着以下内容将起作用:

for (auto &x : {a,b,c}) fun(x);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM