Skip to content

Instantly share code, notes, and snippets.

@jasonlvhit
Created July 16, 2014 15:39
Show Gist options
  • Save jasonlvhit/18ecb126094a537f65a8 to your computer and use it in GitHub Desktop.
Save jasonlvhit/18ecb126094a537f65a8 to your computer and use it in GitHub Desktop.
#include<cstdarg>
#include<iostream>
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;
}
int main()
{
cout << add(1, 2, 3, 0) << endl; //必须以0结尾,因为参数列表结束的判断条件是读到0停止
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment