1. 程式人生 > >C++函數參數中的省略號

C++函數參數中的省略號

!= string param fir idt 傳遞 參數 urn targe

本文基本是轉載自:https://blog.csdn.net/think12/article/details/5785066

另一篇看到寫得很好的博客:https://www.cnblogs.com/haoyuanyuan/p/3221463.html

C++允許定義形參個數和類型不確定的函數。例如,C語言中的標準函數printf便使用這種機制。在聲明不確定形參的函數時,形參部分可以使用省略號 “…”代替。“…”告訴編譯器,在函數調用時不檢查形參類型是否與實參類型相同,也不檢查參數個數。

對於可變參數的函數,需要進行特殊的處理。首先需要引用 <stdarg.h> 頭文件,然後利用va_list類型和va_start、va_arg、va_end 3個宏讀取傳遞到函數中的參數值。

這 幾個宏的定義如下(在 ANSI C 中):
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
void va_start( va_list arg_ptr, prev_param );

說明如下:
va_start
sets arg_ptr to the first optional argument in the list of arguments passed to the function. The argument arg_ptr must have va_list type. The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro’s behavior is undefined. va_start must be used before va_arg is used for the first time.

【 va_start函數將參數arg_ptr設置為可變參數列表的第一個參數。參數arg_ptr的類型必須為va_list。參數prev_param是 在可變參數列表之前的那一個參數。(也就是說在 ANSI C 中,如果一個函數有可變參數,那麽在該可變參數前必須有一個明確定義的參數,否則無法調用函數 va_start ,例如函數 int add(int i,...)是合法的,而函數 int add(...)是不合法的。 )】
va_arg
retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve arguments from the list.
【 va_arg函數將返回 arg_ptr 所指位置的值,並將 arg_ptr 指向下一個參數 】
va_end
After all arguments have been retrieved, va_end resets the pointer to NULL.

示例代碼:

#include<cstdarg>
#include<iostream>
#include<string.h>
using namespace std;


int add(int pre,...)  //求和函數
{
    va_list arg_ptr;

    int sum=0;
    int nArgValue;

    sum+=pre;

    va_start(arg_ptr,pre);
    do
    {
        nArgValue=va_arg(arg_ptr,int);
        sum+=nArgValue;
        
    }while(nArgValue!=0);   //自定義結束條件是輸入參數為0

    va_end(arg_ptr);

    return sum;
}

bool start_with(const char *haystack, const char *needle)
{
    if (NULL == haystack || NULL == needle)
    {
        return false;
    }
    const char *p = strstr(haystack, needle);
    if (p && p == haystack)
        return true;
    else
        return false;
}

bool start_with(const char *haystack,const char *prefix, const char *notprefix1, ...)
{
    bool bStart = start_with(haystack, prefix);
    if (!bStart)
    {
        return false; //若不是以 prefix 結尾則返回false
    }

    bool bFlag = true;
    va_list arg_ptr;
    va_start(arg_ptr, notprefix1);
    while (0 != notprefix1)  //自定義結束條件是輸入參數為NULL
    {
        bStart = start_with(haystack, notprefix1);
        if (bStart)
        {
            bFlag = false;  //若以 notprefix 結尾則返回false
            break; 
        }

        notprefix1 = va_arg(arg_ptr,const char*);
    }
    va_end(arg_ptr);

    return bFlag;
}
int main()
{
   cout<<add(1,2,3,0)<<endl;  //必須以0結尾,因為參數列表結束的判斷條件是讀到0停止
   
   cout << start_with("Chui.mid","C",NULL) << endl;
   cout << start_with("Chui.mid","C",NULL) << endl;
   cout << start_with("Chui.mid","C","Ch",NULL) << endl;
   cout << start_with("Chui.mid","C","CR",NULL) << endl;
   return 0;
}

運行結果:

技術分享圖片

其中比較坑的一個地方在於沒有辦法判斷是不是到了最後一個參數(沒找到相關資料,若有大神知道如何判斷,希望能分享一下),導致我在函數最後必須得添加一個參數用於判斷到了最後一個參數。

技術分享圖片

C++函數參數中的省略號