zoukankan      html  css  js  c++  java
  • 可变参数实践

    参考1:https://www.cnblogs.com/edver/p/8419807.html

    参考2:https://blog.csdn.net/iynu17/article/details/51588199

    参考3: https://www.cnblogs.com/fanzhidongyzby/p/3519838.html

    #include <iostream>
    #include <stdarg.h>
    #include <stdio.h>
    
    
    using namespace std;
    
    //============================================================================
    //总结:
    //============================================================================
    
    
    void fun(int paramCount, ...){
        cout<<"参数个数:"<<paramCount<<endl;
    
        va_list params;
        va_start(params, paramCount);
    
        while(paramCount--){
    
            char* strParam = va_arg(params, char*);
            int intParam = va_arg(params, int);
            cout<< strParam << endl;
            cout<< intParam << endl;
        }
        va_end(params);
    }
    
    
    void print11(const char *format, ...)
    {
        va_list arg;
        va_start(arg, format);
    
        while (*format)
        {
            char ret = *format;
            if (ret == '%')
            {
                switch (*++format)
                {
                case 'c':
                {
                            char ch = va_arg(arg, char);
                            putchar(ch);
                            break;
                }
                case 's':
                {
                            char *pc = va_arg(arg, char *);
                            while (*pc)
                            {
                                putchar(*pc);
                                pc++;
                            }
                            break;
                }
                default:
                    break;
                }
            }
            else
            {
                putchar(*format);
            }
            format++;
        }
        va_end(arg);
    }
    
    int main(){
    
        cout << "函数可变参数实践:" << endl;
    
        // 2指2对参数
        fun(2,"Welcome1:",1,"Welcome2:",2);
    
    
        print11("%s %s %c%c%c%c%c!
    ", "welcome", "to", 'C', 'h', 'i', 'n', 'a');
    
    
        cout << "end." << endl;
    
        return 0;
    }

    问题:

  • 相关阅读:
    今日总结
    今日总结
    今日总结
    今日总结
    今日总结
    java自学
    java自学
    Java自学
    Java自学
    java自学
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11110959.html
Copyright © 2011-2022 走看看