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

How do I write functions that accept unlimited arguments?(如何编写接受无限参数的函数?)
本文介绍了如何编写接受无限参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have only been able to find one way for functions to take a variable amount of arguments.
It's this way:

#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;
}

2 Problems:
1.) I have to specify how many arguments I'm sending in- not desirable
2.) I can't figure out how to modify it so it will output strings.

Would something like this be possible without having to overload the function multiple times:

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
}

What about this:

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

}

All I can think to do is:
-overload the function multiple times
-have the function accept some type of container instead

If this is NOT POSSIBLE, could someone explain why the compiler is not capable of accomplishing a task like this.

解决方案

With variadic templates in C++11, you can do something like this (see the result at 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;
}

这篇关于如何编写接受无限参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)