草庐IT

back-end

全部标签

c++ - push_back() "new"一个对象在添加到 c++ 中的 std::list 之前

我是C++标准库的新手。我想使用std::list。我知道如果我自己创建一个列表而不是使用STL,我应该为一个新对象分配内存,然后将它添加到列表中。A类的C风格列表:A*ptrA=newA();ptrA->setElement(value);ptrA->next=null;currentPositionMyCstyleList->next=ptrA;ptrA->prev=currentPositionMyCstyleList;如果我使用STL,是否有必要“新建”一个对象?push_back()在添加到c++中的std::list之前是否“新建”了一个对象?下面的代码是否正确?AaObj

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++ - 为什么 list::push_back 在 VC++ 中比在 g++ 中慢得多?

此代码在我的VS2012中大约需要20秒,但在G++中仅需1.x秒。均在win8x64中并使用默认选项编译。listitems;for(inti=0;i是关于内存分配的吗?在我的机器上用VC++输出后释放内存需要3~5秒,而在我friend的(win7x64)上甚至超过1分钟。 最佳答案 嗯...我扩展了您的代码以包含计时:#include#include#include#includeintmain(){std::listitems;clock_tstart=clock();for(inti=0;i我用VC++编译使用:cl/O2

c++ - Back_inserter 或 push_back

只是一个简单的问题-将字符串添加到vector的末尾哪个更好?,back_inserter或push_back?主要是,哪个工作得更快(我正在处理大量数据,所以边际差异实际上很重要),主要差异是什么? 最佳答案 两者并不等价。您使用std::back_inserter例如,当您需要将输入迭代器传递给算法时。std::vector::push_back在这种情况下不是一个选择。例如std::vectora(100,"Hello,World");std::vectorb;std::copy(a.begin(),a.end(),std::

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++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

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