【问题标题】:is there a c++ version of int..args [duplicate]是否有 c++ 版本的 int..args [重复]
【发布时间】:2021-10-09 04:21:51
【问题描述】:

Java 我们有variable arguments 这个语法告诉编译器fun() 可以用零个或多个参数调用。因此,这里的 a 被隐式声明为 int[] 类型的数组。

class Test1
{
    // A method that takes variable number of integer
    // arguments.
    static void fun(int ...a)
    {
        System.out.println("Number of arguments: " + a.length);
  
        // using for each loop to display contents of a
        for (int i: a)
            System.out.print(i + " ");
        System.out.println();
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Calling the varargs method with different number
        // of parameters
        fun(100);         // one parameter
        fun(1, 2, 3, 4);  // four parameters
        fun();            // no parameter
    }
}

我希望在C++ 中实现相同的功能,但我找不到任何有用的东西。有人有什么想法吗?

【问题讨论】:

标签: java c++ algorithm data-structures


【解决方案1】:

从 C++11 及更高版本开始,您可以使用 parameter packs 并将它们存储在 std::initializer_list 中:

#include <iostream>
#include <initializer_list>

template <typename ...Args>
void fun(Args const&... args) {
    std::initializer_list<int> arg_list { args... };
    std::cout << "Number of arguments: " << arg_list.size() << std::endl;

    for (auto const& i : arg_list)
        std::cout << i << " ";
    std::cout << std::endl;
}

int main() {
    // Calling the varargs method with different number
    // of parameters
    fun(100);         // one parameter
    fun(1, 2, 3, 4);  // four parameters
    fun();            // no parameter
    // fun(1, "");    // Error
}

【讨论】:

  • 我在这种情况下所做的与此类似。但我更喜欢简单的 C 数组,而不是 initializer_list 变量。然后将该数组与sizeof...(args) 一起传递给实现该功能的私有非模板函数。类似const int arg_list[]{ args... }; fun_impl(arg_list, sizeof...(args)); 优势:无论不同模板参数的数量如何,工作函数都只有一个实例化。
【解决方案2】:

您正在寻找variadic function。请注意,由于这些在 C++ 中实现的方式,实际使用参数需要一系列相当复杂的宏调用,这与 Java 将参数简单地转换为数组(可以报告自己的长度)的做法形成对比。

可变参数模板是一个类似的概念,但其操作本质上是执行自动复制和粘贴,以在编译时实际创建多个不同的函数。

【讨论】:

  • 你能告诉我const char* fmt..到底是什么吗?
【解决方案3】:

我认为这对你来说是一个很好的例子!还有details

#include <iostream>
#include <cstdarg>
void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid
{
    va_list args;
    va_start(args, fmt);
 
    while (*fmt != '\0') {
        if (*fmt == 'd') {
            int i = va_arg(args, int);
            std::cout << i << '\n';
        } else if (*fmt == 'c') {
            // note automatic conversion to integral type
            int c = va_arg(args, int);
            std::cout << static_cast<char>(c) << '\n';
        } else if (*fmt == 'f') {
            double d = va_arg(args, double);
            std::cout << d << '\n';
        }
        ++fmt;
    }
 
    va_end(args);
}
int main()
{
    simple_printf("dcff", 3, 'a', 1.999, 42.5); 
}

【讨论】:

    【解决方案4】:

    如果你想使用多个 int 参数,并且你知道它们的最大数量,你可以使用默认值。例如:

    void fun(int arg1=0,int arg2=0,int arg3=0,int arg4=0)
    {
        ...
    }
    

    或者可以参考main函数的声明:

    int main(int argc,char* argv[]) 
    

    定义类似的函数:

    void fun(int argc, int* argv[])
    {
        ...
    }
    

    【讨论】:

    • 你能告诉我如何访问fun() 中的参数吗?
    猜你喜欢
    • 2011-05-07
    • 2018-11-10
    • 2018-09-22
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2021-08-27
    • 2015-11-21
    • 2022-01-20
    相关资源
    最近更新 更多