本文基本是轉載自:https://blog.csdn.net/think12/article/details/5785066html
另外一篇看到寫得很好的博客:http://www.javashuo.com/article/p-hrtnwdyg-ds.htmlios
C++容許定義形參個數和類型不肯定的函數。例如,C語言中的標準函數printf便使用這種機制。在聲明不肯定形參的函數時,形參部分可使用省略號 「…」代替。「…」告訴編譯器,在函數調用時不檢查形參類型是否與實參類型相同,也不檢查參數個數。函數
對於可變參數的函數,須要進行特殊的處理。首先須要引用 <stdarg.h> 頭文件,而後利用va_list類型和va_start、va_arg、va_end 3個宏讀取傳遞到函數中的參數值。ui
這 幾個宏的定義以下(在 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 );spa
說明以下:
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..net
示例代碼:3d
#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; }
運行結果:code
其中比較坑的一個地方在於沒有辦法判斷是否是到了最後一個參數(沒找到相關資料,如有大神知道如何判斷,但願能分享一下),致使我在函數最後必須得添加一個參數用於判斷到了最後一個參數。htm