本文基本是转载自: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. [CentOS] 常用工具软件包

    gcc & g++ & gdb • 安装方法 yum install gcc -y yum install gcc-c++ -y         yum install gdb -y ...

  2. windows计划任务定时运行synctoy的坑

    每次设置好synctoy之后,需要让synctoy运行一次,windows的计划任务才能成功执行,如果变更了synctoy的设置,而没有让synctoy成功执行过,windows计划任务将执行失败,坑 ...

  3. linux_rpm命令

    rpm 常用命令 1.安装一个包 # rpm -ivh 文件名 2.升级一个包 # rpm -Uvh文件名 3.移走一个包 # rpm -e文件名 4.安装参数 --force 即使覆盖属于其它包的文 ...

  4. Netflix Hystrix — 应对复杂分布式系统中的延时和故障容错 转

    转自 https://segmentfault.com/a/1190000005988895 前言 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为 ...

  5. vultr VPS安装BBR

    1.安装 wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh chmod +x bbr.s ...

  6. spring手动配置

    本文总结自:https://www.cnblogs.com/V1haoge/p/7183408.html SpringBoot中免除了大部分配置,但是对于一些特定的情况,还是需要我们进行手动配置的. ...

  7. 20145216史婧瑶《Java程序设计》第四次实验报告

    实验四 Android环境搭建 实验内容 搭建Android环境 运行Android 修改代码,能输出学号 实验步骤 1.搭建Android环境 2.安装Android,核心是配置JDK.SDK 3. ...

  8. 20145321 《Java程序设计》第3周学习总结

    20145321 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 4.1 类与对象 1.定义类:类定义时用关键词 class. 一个原始码中可以有多个类定义,但只能有一个公开 ...

  9. Android Device Monitor 文件管理使用的常见问题

    本文参照博客:http://blog.csdn.net/aurorayqz/article/details/65705300.以下是我的实际操作. 1.使用Android Device Monitor ...

  10. Mysql uploader File

    前几天和哥们做一次渗透测试,内网情况.防护相当严格. 内网不允许访问DMZ,DMZ不允许访问内网,除了服务端口,比如80,3306. 经过长时间的分析,就发现本机连接了内网的一个MYQL,发现WEB开 ...