C++函数参数中的省略号
本文基本是转载自: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++函数参数中的省略号的更多相关文章
- python函数参数中带有默认参数list的坑
		
在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 def f(x,l=[]): for i in range(x): l.append(i*i) print(l) print( ...
 - (转)python中函数参数中如果带有默认参数list的特殊情况
		
在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 1 2 3 4 5 6 7 8 9 def f(x,l=[]): for i in range(x): ...
 - 关于cmp函数参数中的&符号
		
关于cmp函数参数中的&符号 关于sort函数中的cmp函数有着不同的写法,以刚刚的整形元素比较为例 还有人是这么写的: bool cmp(const int &a, const in ...
 - Python函数参数中的冒号与箭头
		
在一些Python的工程项目中,我们会看到函数参数中会有冒号,有的函数后面会跟着一个箭头,你可能会疑惑,这些都是什么东西? 其实函数参数中的冒号是参数的类型建议符,告诉程序员希望传入的实参的类型.函数 ...
 - Delphi 中 函数参数中的 const 修饰符的本质以及注意事项
		
来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...
 - javascript 在一个函数参数中包含另一个函数的引用
		
javascript函数的参数包含另一个函数的情形: <script> //b函数的参数func为另一个函数 function b(a, func) { alert(a); //调用参数 ...
 - 【VS开发】程序如何捕捉signal函数参数中指定的信号
		
当说到signal的功能时,我们都知道它会捕捉我们所指定的信号,然后调用我们所指定的信号处理函数.但它是如何捕捉我们指定的信号的呢?下面我就以msdn上关于signal的example为例,说明sig ...
 - C++ 函数参数中“ *&代表什么? ”
		
typedef struct BitNode { char value; BitNode *lchild,*rchild; }BitNode,*BiTree; void C ...
 - C# 函数参数中的this
		
先看下面的代码: public static class StringExtension { public static void Foo(this string s) { Console.Write ...
 
随机推荐
- (转)VC串口小程序(用SerialPort类)
			
××××××××××××××××××××××××××××××××××××××××××××××××××××× 在MFC里面实现串口通讯有很多方式: 方案一:使用微软公司提供的 串口类,SerialPor ...
 - PEP8编码规范
			
1.代码布局设计 1.1 缩进 -4个空格进行缩进 1.2 tab键-在python2中tab和空格是混用的,但是在python中基本上使用tab(pycharm开发工具会自动对代码缩进) 1.3 最 ...
 - ELK日志系统
			
ELK stack是又Elasticsearch,lostash,kibana 三个开源软件的组合而成,形成一款强大的实时日志收集分析展示系统. Logstash:日志收集工具,可以从本地磁盘,网络服 ...
 - Twitter的分布式自增ID算法snowflake
			
snowflake 分布式场景下获取自增id git:https://github.com/twitter/snowflake 解读: http://www.cnblogs.com/relucent/ ...
 - 1 安装企业wiki:confluence
			
使用wget下载命令下载文件. 下载成功使用dir可以看到文件目录 [root@localhost usr]# diratlassian-confluence-6.3.1-x64.bin bin ...
 - java多态 以及静态绑定  动态绑定积累
			
重载,英文名是overload,是指在一个类中定义了一个以上具有相同名称的方法,这些方法的参数个数.参数类型和顺序不能相同.返回类型可以相同,也可以不同. 重写,英文名是overrid,是指在继承情况 ...
 - ThinkPHP项目 公共方法存放位置
			
ThinkPHP项目公共方法写在 根目录-> app-> common 里面 ThinkPHP模板公共方法卸载 根目录->app->模块名称->common 里 ...
 - Spark高级数据分析· 6LSA
			
潜在语义分析 wget http://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles-multistream.xml.bz ...
 - GRUB2 分析 (三)
			
接上一篇 从地址0x8200开始的是lzma_decompress.img.这是由startup_raw.S编译生成的.这个文件稍微复杂点.首先一开始就是个跳转指令: ljmp $0, $ABS(LO ...
 - 前端学习笔记之Z-index详解
			
CSS当中的z-index属性看起来足够简单,但是如果你真的想了解它是如何工作的话,在这简单的表面之下,又有许多值得探究的内容. 在这篇教程中,通过探究层叠上下文和一系列实际的例子,我们将会阐明z-i ...