草庐IT

c++ - 如何在 va_start 中使用字符串?

出于某种原因,我无法正常工作:voidexamplefunctionname(stringstr,...){...va_start(ap,str.c_str());我也没有得到这个工作:voidexamplefunctionname(stringstr,...){...intlen=str.length();char*strlol=newchar[len+1];for(inti=0;i但这确实:voidexamplefunctionname(constchar*str,...){...va_start(ap,str);有人可以告诉我如何使用字符串代替constchar*吗?当我调用ex

c++ - 抛出异常时是否需要 va_end?

我有一个基于printf样式格式的日志记录框架:voidLogger::debug(constchar*fmt,...){va_listargs;va_start(args,fmt);this->output(DebugLevel,fmt,args);va_end(args);}如果Logger::output抛出异常,编译器是否会正确展开堆栈,或者我是否需要在捕获条款?这可以改为RAII,还是va_end太神奇了?如果可能,请包括对标准的引用。 最佳答案 不,他们不能。因为它们是宏而不能的推理是愚蠢的。宏可以毫无问题地从构造函数和

c++ - m.find(...) == m.end() - 使用的是 iterator 或 const_iterator

std::mapfind/end都提供const_iterator和迭代器,例如iteratorend();const_iteratorend()const出于好奇,如果我有一个std::map,它将在这里被调用/比较,一个迭代器或一个const_iterator?:if(m.find(key)!=m.end()){...}我应该关心吗? 最佳答案 如果m是const,则返回一个const_iterator;否则将返回一个迭代器。如果您所做的只是测试map中是否存在某个元素,那么使用哪个元素并不重要。

c++ - begin(container) 和 end(container) 是否标准化?

非成员函数模板begin(container)和end(container)是C++0x的一部分吗?如果是这样,它们位于哪个头文件中? 最佳答案 是的,但就像swap一样定义在不同的地方并取决于ADL,begin也是如此和end.“通用”版本在中定义://24.6.5,rangeaccess:templateautobegin(C&c)->decltype(c.begin());templateautobegin(constC&c)->decltype(c.begin());templateautoend(C&c)->decltyp

c++ - 理论上,find_end 是可并行化的吗?

我目前正在研究open-stdproposal为我正在处理的项目带来并行功能,但我遇到了find_end的障碍。现在find_end可以描述为:Analgorithmthatsearchesforthelastsubsequenceofelements[s_first,s_last)intherange[first,last).Thefirstversionusesoperator==tocomparetheelements,thesecondversionusesthegivenbinarypredicatep.它的要求由cppreference列出.现在我并行化find/findi

c++ - end() 迭代器的算术运算

设A为std::vector,这是明确的吗?if(!A.empty())std::vector::iteratormyBack=A.end()-1;是end迭代器只适用于等式和不等式检查?或者只要我留在容器中,我就可以执行一些指针运算?在我的平台上,此代码有效。我想知道这是否是可移植的。 最佳答案 它是完全有效的,因为vector::iterator是一个随机访问迭代器。您可以对其执行算术运算,并且它不依赖于平台。std::vector::iteratorit=A.end();while(it!=A.begin()){--it;//

c++ - 在 C++ 中处理 va_args

我有一个函数A(...)和B(...)。现在我必须在A中调用B,任何将...从A传递到的方法>B?伪代码:voidA(...){//SomeoperatorsB(...);//Insteadof...IneedtopassA'sargs}附注我知道这可以使用宏来完成,但是函数呢。 最佳答案 您不能转发va_args。您只能转发va_list。voidvB(intfirst,va_listap){//dostuffwithap.}voidB(intfirst,...){va_listap;va_start(ap,first);vB(f

c++ - std::begin 和 std::end 不能使用指针和引用,为什么?

为什么std::begin()和std::end()使用数组而不是指针[这几乎是数组]和数组的引用[这是原始数组的别名]。挠头15分钟后,我无法在谷歌中得到任何东西。下面只有第一种情况有效,第二种和第三种情况无效,这可能是什么原因?#include#include#include#includeintmain(){intfirst[]={5,10,15};//FistCaseif(std::find(std::begin(first),std::end(first),5)!=std::end(first)){std::cout错误:error:nomatchingfunctionfor

c++ - 无法使用 end() 获取 map 的第二个字段

我正在创建一个map,只是为了学习目的来存储一些键值对。如果我使用begin()函数打印map的第二个字段,我可以打印map的第二个字段,但是当我尝试使用end()它无法打印第二个字段。下面是我的代码:#include#include#include#include#includeusingnamespacestd;maparr;map::iteratorp;intmain(intargc,char**argv){arr[1]="Hello";arr[2]="Hi";arr[3]="how";arr[4]="are";arr[5]="you";p=arr.begin();printf(

c++ - C++11 可变参数模板中的 va_arg() 是什么?

这个问题在这里已经有了答案:C++indexoftypeduringvariadictemplateexpansion(4个答案)关闭8年前。我已经阅读了一些关于这个新的C++11功能的文章,但我并不理解所有的东西(我是C++的新手)。如何访问特定参数,就像在C中使用stdarg.h中的va_arg一样?templatevoidf(Args...args){for(size_ti=0;i