如何编写接受无限参数的函数?

Tre*_*key 18 c++ variables parameters arguments function

我只能找到一种方法来使函数获取可变数量的参数.
就是这样:

#include <iostream>
#include <stdarg.h>

using namespace std;

void Print(int argumentAmount, ... );

int main()
{
    Print(5,11,22,33,44,55);
}

void Print(int argumentAmount, ... ){

    va_list arguments; 
    va_start(arguments, argumentAmount);

    int parameter;
    for(int i = 0; i < argumentAmount; ++i ){
        parameter = va_arg(arguments, int);
        cout << parameter << endl;
    }

    va_end(arguments);
    return;
}
Run Code Online (Sandbox Code Playgroud)

2问题:
1.)我必须指定我发送了多少个参数 - 不可取
2.)我无法弄清楚如何修改它以便输出字符串.

这样的事情是可能的,而不必多次重载函数:

void Output(/*not sure how this would look*/);

int main(){

    Output("hello","world");
    Output("this","is","a","test");
    Output("As","many","strings","as","you","want","may","be","passed","in");

    return 0;
}
void Output(/*not sure how this would look*/){

    //loop through each string passed in and output it
}
Run Code Online (Sandbox Code Playgroud)

那这个呢:

void Capitalize(/*all passed by reference*/);

int main(){

    string s1 = "hello";
    string s2 = "world";

    string s3 = "this";
    string s4 = "is";
    string s5 = "a";
    string s6 = "test";

    string s7 = "as";
    string s8 = "many";
    string s9 = "strings";
    string s10 = "as";
    string s11 = "you";
    string s12 = "want";

    Capitalize(s1,s2);
    Capitalize(s3,s4,s5,s6);
    Capitalize(s7,s8,s9,s10,s11,s12);

    return 0;
}
void Capitalize(/*all passed by reference*/){

    //capitalize each string passed in

}
Run Code Online (Sandbox Code Playgroud)

我所能想到的是: -
多次
重载函数 - 函数接受某种类型的容器

如果这是不是可能的,可能有人解释为什么编译器不能够完成这样的任务.

Ale*_*nov 41

使用C++ 11中的可变参数模板,您可以执行以下操作(请参阅ideone的结果)

#include <string>
#include <iostream>

void Output() {
    std::cout<<std::endl;
}

template<typename First, typename ... Strings>
void Output(First arg, const Strings&... rest) {
    std::cout<<arg<<" ";
    Output(rest...);
}

int main() {
    Output("I","am","a","sentence");
    Output("Let's","try",1,"or",2,"digits");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 哇,我想您只是帮助我了解了可变参数模板。 (2认同)

kob*_*las 12

快速简单的答案.

对于C++,您需要指定参数的数量或指定参数结束的sentinel值.

你的第一个例子是计算计数的一个很好的例子,你也可以这样做:

void Print(const char *arg, ... ){
    va_list arguments;

    for (va_start(arguments, arg); arg != NULL; arg = va_arg(arguments, const char *)) {
        cout << arg << endl;
    }

    va_end(arguments);
}
Run Code Online (Sandbox Code Playgroud)

你的召集惯例是:

Print("foo","bar",NULL);
Run Code Online (Sandbox Code Playgroud)

如果你想把它提升到一个新的水平,你可以混合一点C预处理器并做:

#define mPrint(...) Print(__VA_ARGS__, NULL)
Run Code Online (Sandbox Code Playgroud)

现在你可以说:

mPrint("fooo","bar");
Run Code Online (Sandbox Code Playgroud)

宏将NULL终止呼叫.


Ale*_*nze 6

您可以使用特殊的"尾随"参数(nullptr或指向某些硬编码"魔术"字符串的指针)而不是传递计数,并且您的变量参数函数应该在看到尾随参数时停止提取更多参数.这可以简化您的编码.

您还可以将指针(引用)传递给包含(或指向/引用)字符串的容器.任何可以某种方式链接你所有个人参数的东西都会做(例如矢量).

示例(可能不是非常惯用,但应作为说明):

#include <iostream>
#include <string>
#include <cstdarg>
#include <cctype>
#include <vector>

using namespace std;

void AntiCapitalize(vector<string*>& v);
void Capitalize(string* s, ...);
void Print(string* s, ...);

int main()
{
    string s1 = "hello";
    string s2 = "world";

    string s3 = "this";
    string s4 = "is";
    string s5 = "a";
    string s6 = "test";

    string s7 = "as";
    string s8 = "many";
    string s9 = "strings";
    string s10 = "as";
    string s11 = "you";
    string s12 = "want";

    Capitalize(&s1, &s2, 0);
    Capitalize(&s3, &s4, &s5, &s6, 0);
    Capitalize(&s7, &s8, &s9, &s10, &s11, &s12, 0);

    Print(&s1, &s2, 0);
    Print(&s3, &s4, &s5, &s6, 0);
    Print(&s7, &s8, &s9, &s10, &s11, &s12, 0);

    vector<string*> v;
    v.push_back(&s1);
    v.push_back(&s2);
    v.push_back(&s3);
    v.push_back(&s4);
    v.push_back(&s5);
    v.push_back(&s6);
    v.push_back(&s7);
    v.push_back(&s8);
    v.push_back(&s9);
    v.push_back(&s10);
    v.push_back(&s11);
    v.push_back(&s12);

    AntiCapitalize(v);

    Print(&s1, &s2, 0);
    Print(&s3, &s4, &s5, &s6, 0);
    Print(&s7, &s8, &s9, &s10, &s11, &s12, 0);

    return 0;
}

void Capitalize(string* s, ...)
{
    va_list ap;

    va_start(ap, s);

    while (s)
    {
        string::size_type i = 0;

        while ((*s)[i] != '\0')
        {
            (*s)[i] = toupper((*s)[i]);
            i++;
        }

        s = va_arg(ap, string*);
    }

    va_end(ap);
}

void Print(string* s, ...)
{
    va_list ap;

    va_start(ap, s);

    while (s)
    {
        cout << *s << endl;
        s = va_arg(ap, string*);
    }

    va_end(ap);
}

void AntiCapitalize(vector<string*>& v)
{
    vector<string*>::iterator it;

    for (it = v.begin(); it != v.end(); it++)
    {
        string::size_type i = 0;

        while ((**it)[i] != '\0')
        {
            (**it)[i] = tolower((**it)[i]);
            i++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

HELLO
WORLD
THIS
IS
A
TEST
AS
MANY
STRINGS
AS
YOU
WANT
hello
world
this
is
a
test
as
many
strings
as
you
want
Run Code Online (Sandbox Code Playgroud)