C++变长参数函数的用法(转)

c/c++ 里面的变长参数的实现   c里面的变长参数,c++里面也有。提供了:一个类型  va_list3个宏  va_start  va_arg  va_end使用还是很方便的,但是实现是怎么样的呢? 阅读详情

书上说,当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表
(...)

如:void foo(...);
     void foo(parm_list,...);
void foo(...)
{
    //...
}
调用:foo(a,b,c);

就是不懂,把a,b,c的值传进函数里面后,用什么变量来接收???如果不能接收,(...)岂不是没意义?
还有就是不明白
int printf(const char*...);
printf("hello,&s/n",userName);

这个c的输出函数是怎么用(...)实现的.

先谢了:)

Re:

首先函数体中声明一个va_list,然后用va_start函数来获取参数列表中的参数,使用完毕后调用va_end()结束。像这段代码:
void TestFun(char* pszDest, int DestLen, const char* pszFormat, ...)
{
va_list args;
va_start(args, pszFormat);
_vsnprintf(pszDest, DestLen, pszFormat, args);
va_end(args);
}

 

还记得printf函数调用的时候那个“...”吗?就是可以输入任意的参数。现在你用va_list也可以实现类似的函数声明,printf就是这样做的。

va_list args;                                                     //声明变量
va_start(args,  before);                                  //开始解析。args指向before后面的参数 
参数类型 var = va_arg(args,  参数类型);     //取下一个参数并返回。args指向下一个参数
va_end(args);                                                  //结束解析

c/C++可变参数函数的参数传递机制剖析 文章对可变参数函数的参数传递机制进行了剖析, 给出了准确、灵活设计可变参数函数的另一种方法 立即下载

相关推荐

Windows 11下ROS2 Humble与PyCharm无缝集成实战(避坑指南+完整配置流程)

本文详细介绍了在Windows 11系统下如何实现ROS2 Humble与PyCharm的无缝集成,包括环境准备、ROS2 Humble安装、PyCharm配置以及C++与Qt5混合开发的实战技巧。通过避坑指南和完整配置流程,帮助开发者高效搭建稳定的机器人开发环境,特别适合Windows平台的ROS2开发者。

weixin_28710999的博客 373

C++/C++11中变长参数的使用

C++/C++11中变长参数的使用

网络资源是无限的 3万+

手把手教你用P-MOS管搭建双电源自动切换电路(含AO3401选型指南)

本文提供了一份基于P-MOS管AO3401构建高可靠双电源自动切换电路的实战指南。文章详细对比了继电器、理想二极管与MOS管方案的优劣,深入解析了AO3401的关键参数选型,并手把手指导了从原理图设计、PCB布局到上电调试的全过程,旨在帮助硬件工程师和创客以极低成本实现无缝、高效的电源冗余方案。

moss5的博客 1269

C/C++ 中那些可变长参数

C/C++提供了一些处理可变长参数/扩展参数包的宏、函数、模板,本文主要是记录下他们的使用方式 1. 使用<stdarg.h>中的宏 stdarg.h 头文件定义了一个变量类型 va_list 和三个宏,这三个宏可用于在参数个数未知(即参数个数可变)时获取函数中的参数。可变参数的函数通在参数列表的末尾是使用省略号(,...)定义的。很多C语言库都是使用这种方式来处理输入参数列表的。...

龚建波 6438

6 赫斯曼网管软件industrial hivision用户管理

文章目录前言一、打开Industrial HiVision服务器二、在用户管理界面添加策略三、使用可选策略四、接受策略总结 前言 安装 Industrial HiVision 后,所有人都能广泛使用程序。安全起见,请进行相 关配置,对访问权限进行限制。 本文主要介绍用户管理基于某种角色模型。在访问角色中定义角色名称,通过分配访问权限给这些角色分配角色功能。例如,给管理人员自然要分配可用的权限,而观察员尽管可以登录,但没有能够进行更改的权限。Industrial HiVision 提供以下访问角色: 

t98746194的博客 1384

Kylin-Desktop-V10-SP1-General-Release-2303-X86_64-海光版(hygon c86)安装与配置指南

本文提供了在国产海光C86处理器平台上安装与配置Kylin Desktop V10 SP1操作系统的详细指南。内容涵盖从下载专用镜像、制作启动盘、BIOS关键设置、磁盘分区方案,到系统安装后的驱动优化、必备软件安装及常见故障排除,旨在帮助用户顺利完成hygon c86平台的系统部署与优化。

tree的博客 1415

C++ 函数传递多参数处理 可变参数模板

1、initializer_list形参 如果函数的实参数量未知但是全部实参的类型都相同,我们可以使用initializer_list类型的形参(C++11新标准) initializer_list提供的一些操作: #include<initializer_list> // 头文件 initializer_list lst; // 默认初始化,T类型元素的空列表 initializer_list lst{a,b,c…}; // 初始化为初始值列表的副本 lst2(lst) // 拷贝或赋

qq_24447809的博客 7111

SAP 配额修改 + 修改记录写入实现

采购配额修改并写入修改记录 cdhdr 和 cdpos

GAVIN 1117

C++ 变长参数列表函数的用法

<br />对变长参数的访问是通过下面三个宏实现的:<br />void va_start( va_list arg_ptr, prev_param ); // (ANSI version) type va_arg( va_list arg_ptr, type ); void va_end( va_list arg_ptr ); <br />参数 <br />type: 待获取参数的数据类型; <br />arg_ptr: 指向参数列

生命不息, 战斗不止! 7040

LSTM+Transformer混合模型时间序列预测实战教学

LSTM+Transformer混合模型时间序列预测实战教学 毕业设计

8227

innovus:sroute 双层follow pin的做法

根据std cellpower pin的层次可以分成两种情况。

m0_61544122的博客 2758

C++学习 - 模板函数变长参数

为什么需要变长参数 C变长参数 1 文档 2 C变长参数例子 C模板函数 以下所说编译环境为: Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 7.3.

累了就歇一会 2921

【YOLOv8改进-注意力机制】LSKNet(Large Selective Kernel Network ):空间选择注意力

【YOLOv8改进-注意力机制】LSKNet(Large Selective Kernel Network ):空间选择注意力 (论文笔记+引入代码)。LSKNet能够动态调整其大的空间接收场,以更好地模拟遥感场景中各种对象的范围上下文。据我们所知,这是首次在遥感目标检测领域探索大型和选择性核机制。

专注于图像领域,主要研究内容包括计算机视觉和深度学习,特别是在图像分类、目标检测和图像生成等方面有深入的研究和实践经验。 2866

C/C++要点全掌握(六)——变长参数

上接C/C++要点全掌握(五)——mutable、volatile     14、变长参数          设计一个参数个数可变、参数类型不定的函数是可能的,最常见的例子是printf函数、scanf函数和高级语言的Format函数。在C/C++中,为了通知编译器函数的参数个数和类型可变(即是不定的、未知的),就必须以三个点结束该函数的声明。 // printf函数的声明

TU2的专栏 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

C++中的可变长参数函数

Table of Contents C语言形式可变长参数varargs 用法 Default Argument Promotion(默认参数提升) C++推荐使用的initializer_list 参考文档 C语言形式可变长参数varargs 用法 比较常见的有printf系列的函数,其函数声明一般如下: int add(int count, ...); 在上面的声明中使用…代表函数的未知可变参数,其使用方式如下: int add(int count, ...) { int resu

一期一会 1362

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

C++ 变长参数、折叠表达式

含义:(((init op pack1) op pack2) …含义:(((init op pack1) op pack2) …(packN op init)))含义:((pack1 op pack2) op pack3) …二元右折叠:(arg1 << (arg2 << (arg3 << init)))一元左折叠:(((arg1 + arg2) + arg3) + …二元左折叠:(((100 - arg1) - arg2) - …二元左折叠:(((100 - arg1) - arg2) - …

zzzsss123333的博客 1157

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

c++11新特性:变长参数模板详解

这里写目录标题C++11 变长参数模板变长函数参数包如何解参数包sizeof()获得函数参数个数递归模板函数变参模板展开结论 C++11 变长参数模板 在C++11之前,无论是类模板 还是函数模板,都只能按其指定的样子,接受一组固定数量的模板参数; 这已经大大提升了代码的复用! 在C++11之后,加入了新的表示方 法,允许任意个数、任意类别的模板参数,同时也不需要在定义时将参数的个数固定。更加像”黑魔法“了。 template<typename... Ts> class Magic; 模板类

CodeBowl的博客 5060

c++变长参数

c++变长参数

qq_35985044的博客 1224

C/C++变长参数

C/C++变长参数

大草的博客 1140

C++11:模板函数实现支持变长参数的简单日志输出

开源的世界,现成的轮子很多,但如果现成的轮子太重太复杂,有的时候也不妨自己发明个轻便的轮子用起来更趁手。 经常我们在程序中需要打调试信息或普通的屏幕输出,大多情况情况下,用printf就可以将就了,但printf用志来也不是太方便: 需要为不同的参数类型指定不同的输出格式(%s,%d....),错了还不行,好麻烦,为了调试方便需要在文件名和行号,printf可做不到这个。 我们知道,...

10km的专栏 2399

matlab 变长参数,变长参数函数的概念

分享一个2015年华为笔试知识点:变长参数函数变长参数的函数即参数个数可变、参数类型不定 的函数。设计一个参数个数可变、参数类型不定的函数是可能的,最常见的例子是printf函数、scanf函数和高级语言的Format函数。在C/C++中,为了通知编译器函数的参数个数和类型是可变的(即是不定的、未知的),就必须以三个点结束该函数的声明。intprintf(constchar*_Format...

weixin_35687571的博客 528
上一篇: 内存管理内幕(转)
下一篇: 覆盖GNU C库中调试glibc函数
吴遥
博客等级 码龄19年 90粉丝 93原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址