草庐IT

first_it

全部标签

c++ - regex_token_iterator *it++ 错误?

对于以下代码:#include#includeusingnamespacestd;intmain(intargc,char*argv[]){regexreg("/");strings="Split/Values/Separated/By/Slashes";sregex_token_iteratorit{std::begin(s),std::end(s),reg,-1};sregex_token_iteratorend;while(it!=end){cout应该输出:SplitValuesSeparatedBySlashes但是它输出这个:ValuesSeparatedBySlashes

c++ - "cast to first member of standard layout"类型双关规则是否扩展到数组?

具体来说,我将CAPI包装在一个友好的C++包装器中。CAPI具有这种相当标准的形式:structfoo{...};voidget_foos(size_t*count,foo*dst);我想做的是,通过将类型双关的包装器数组直接传递给Capi来为自己保存一个额外的拷贝,并保持理智检查static_assert().classfooWrapper{fooraw_;public:[...]};std::vectorget_foo_vector(){size_tcount=0;get_foos(&count,nullptr);std::vectorresult(count);//Isthis

c++ - 用已知数量的元素填充 vector : specify its size in constructor or by using reserve method?

我想通过从流中读取单个元素来创建某种复杂类型的vector。我提前知道vector大小。是在vector构造函数中指定元素个数更好,还是使用reserve方法更好?这两个哪个更好?intmyElementCount=stream.ReadInt();vectormyVector(myElementCount);for(inti=0;i或intmyElementCount=stream.ReadInt();vectormyVector;myVector.reserve(myElementCount);for(inti=0;i如果我只是创建一个intvector或其他一些简单类型呢?

c++ - 使用 static_cast 处理混合(基础和派生)对象的 vector 是否存在性能风险? (又名 "it this a dumb idea?")

给定基类gameObject和派生类animatedGameObject,我认为将它们的所有实例存储在std::vector。如果vectorGameObjects声明为gameObject*的基类型,则派生对象实例需要强制转换。例子:vectorGameObjects;gameObjectA*=newgameObject(...init...);animatedGameObjectB*=newanimatedGameObject(...init...);GameObjects.push_back(A);GameObjects.push_back(B);//toaccesstheani

C++ 继承 : Calling virtual method when it has been overridden

我正在尝试构建一个可以在单独的线程中运行(即执行它的run()函数)的service对象。这是服务对象#include#include#include#includeclassservice:publicboost::noncopyable{public:service():stop_(false),started_(false){}virtual~service(){stop();if(thread_.joinable()){thread_.join();}}virtualvoidstop(){stop_=true;}virtualvoidstart(){if(started_.lo

c++ - 作为类成员的可变大小数组 : why does it compile?

我有这种情况,我无法解释它编译的原因:#includeusingnamespacestd;classX{public:X(){cout我正在定义一个可变大小的X数组作为类Y的成员。在类外这样定义X肯定会导致编译错误,但在类内不会。更重要的是,X的构造函数从未被调用。那么这里发生了什么? 最佳答案 C99,6.7.2.1/16(n1256)Asaspecialcase,thelastelementofastructurewithmorethanonenamedmembermayhaveanincompletearraytype;thi

c++ - 提神气 : Take a rule's attribute and set it as a field of an enclosing rule's struct attribute?

像许多其他问题一样,我正在尝试使用Boost.Spirit.Qi将简单语法解析为结构树。我会尽量提炼我正在尝试做的事情,以尽可能最简单的情况。我有:structInteger{intvalue;};BOOST_FUSION_ADAPT_STRUCT(Integer,(int,value))稍后,在语法结构中,我有以下成员变量:qi::ruleinteger;我用它来定义integer=qi::int_;但是,当我尝试实际解析一个整数时,使用qi::phrase_parse(iter,end,g,space,myInteger);myInteger.value在成功解析后始终未初始化。同

c++ - (*it)->method() 与 (**it).method

当遍历指针的vector(或其他容器)时,使用以下优势和/或优势之间是否有任何区别:for(it=v.begin();it!=v.end();++it){(*it)->method();}或for(it=v.begin();it!=v.end();++it){(**it).method();} 最佳答案 在C语言中,没有区别。但是,在C++中,->运算符可以被重载,而成员选择.运算符则不能。所以在(*foo)->bar中*foo可以指定一个充当智能指针的类对象,尽管如果这不会发生foo是标准C++指针容器上的迭代器,这意味着*foo

c++ - 提升 : dereference a template argument if it's a pointer

如果它是一个指针(或智能指针),我可以使用什么来取消引用模板参数,或者如果它不是,我可以保持原样吗?templatevoidsubf(constT&item){item.foo();}templatevoidf(constT&item){subf(magic_dereference_function(item));}Boost中的任何内容都是一个选项。 最佳答案 templateT&maybe_deref(T&x){returnx;}templateT&maybe_deref(T*x){return*x;}您必须单独为智能指针添加重

c++ - 这里的 (void) first2++ 有什么意义?

这个问题在这里已经有了答案:Whydoesstd::transformandsimilarcastthe'for'loopincrementto(void)?(2个答案)关闭5年前。关于thisen.cppreference的页面有可能实现词典顺序比较的示例。这是基本的:templateboollexicographical_compare(InputIt1first1,InputIt1last1,InputIt2first2,InputIt2last2){for(;(first1!=last1)&&(first2!=last2);first1++,(void)first2++){if