本文基本是转载自: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=;
int nArgValue; sum+=pre; va_start(arg_ptr,pre);
do
{
nArgValue=va_arg(arg_ptr,int);
sum+=nArgValue; }while(nArgValue!=); //自定义结束条件是输入参数为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 ( != 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(,,,)<<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 ;
}

运行结果:

其中比较坑的一个地方在于没有办法判断是不是到了最后一个参数(没找到相关资料,若有大神知道如何判断,希望能分享一下),导致我在函数最后必须得添加一个参数用于判断到了最后一个参数。

C++函数参数中的省略号的更多相关文章

  1. python函数参数中带有默认参数list的坑

    在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 def f(x,l=[]): for i in range(x): l.append(i*i) print(l) print( ...

  2. (转)python中函数参数中如果带有默认参数list的特殊情况

    在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 1 2 3 4 5 6 7 8 9 def f(x,l=[]):     for i in range(x):         ...

  3. 关于cmp函数参数中的&符号

    关于cmp函数参数中的&符号 关于sort函数中的cmp函数有着不同的写法,以刚刚的整形元素比较为例 还有人是这么写的: bool cmp(const int &a, const in ...

  4. Python函数参数中的冒号与箭头

    在一些Python的工程项目中,我们会看到函数参数中会有冒号,有的函数后面会跟着一个箭头,你可能会疑惑,这些都是什么东西? 其实函数参数中的冒号是参数的类型建议符,告诉程序员希望传入的实参的类型.函数 ...

  5. Delphi 中 函数参数中的 const 修饰符的本质以及注意事项

    来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...

  6. javascript 在一个函数参数中包含另一个函数的引用

    javascript函数的参数包含另一个函数的情形: <script> //b函数的参数func为另一个函数 function b(a, func) {  alert(a); //调用参数 ...

  7. 【VS开发】程序如何捕捉signal函数参数中指定的信号

    当说到signal的功能时,我们都知道它会捕捉我们所指定的信号,然后调用我们所指定的信号处理函数.但它是如何捕捉我们指定的信号的呢?下面我就以msdn上关于signal的example为例,说明sig ...

  8. C++ 函数参数中“ *&代表什么? ”

    typedef struct BitNode  {      char value;      BitNode *lchild,*rchild;    }BitNode,*BiTree; void C ...

  9. C# 函数参数中的this

    先看下面的代码: public static class StringExtension { public static void Foo(this string s) { Console.Write ...

随机推荐

  1. Java基础知识陷阱(四)

    本文发表于本人博客. 今天我们来说说关于java继承以及反射有关的问题,大家先看下下面代码,试问可以编译通过不,为什么具体说说原因? public class Test{ public static ...

  2. 一步一步学EF系列二【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  3. 如何下载java的jdk

    1.https://www.oracle.com/index.html 2.https://www.oracle.com/downloads/index.html 3.https://www.orac ...

  4. exec-maven-plugin配置及使用

    背景: 如果你想在项maven生命周期内,运行一段java代码,或者一段独立的程序,或者说我们所指的预执行,初始化某些值,生成某些不能预先生成的文件.那么这样我们就可以使用exec-maven-plu ...

  5. 233. Number of Digit One(统计1出现的次数)

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  6. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures

    题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...

  7. Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)

    题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...

  8. ORA-00257: archiver error的解决方法

    背景:多个用户同时做测试数据,有时候突然Oracle系统就崩溃了,然后报一个ORA-00257: archiver error. Connect internal only, until freed的 ...

  9. Javascript中的函数数学运算

    1.Math函数与属性使用语法 Math.方法名(参数1,参数2,...); Math.属性; 说明 Math函数可以没有参数,比如Math.random()函数,或有多个参数,比如Math.max( ...

  10. CSS 图像透明/不透明

    CSS 图像透明/不透明 使用CSS很容易创建透明的图像. 注意:CSS Opacity属性是W3C的CSS3建议的一部分. 一.示例一:创建一个透明图像 CSS3中属性的透明度是 opacity. ...