草庐IT

has_insertion_operator

全部标签

c++ - 在 C++ 的三规则中,为什么 operator= 不调用复制构造函数?

以下“最小”示例应显示ruleof3(andahalf)的用法.#include#includeclassC{std::string*str;public:C():str(newstd::string("defaultconstructed")){std::cout像这样编译它(g++版本:4.7.1):g++-Walltest.cpp-otest现在,应该发生什么?我假设行a=b.get_new();会制作一个硬拷贝,即分配一个新字符串。原因:operator=()按照此设计模式中的典型情况采用其参数,每个值调用一个复制构造函数,该构造函数将进行深层复制。究竟发生了什么?stdcto

c++ - 错误 : invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'

我想获取z_Data的第48个字符的第6位{charc=pPkt->z_Data[47];//thisz_Dataisacharbufferstd::cout>3)&1>4)&1>5)&1 最佳答案 优先级高于&,所以你需要:std::cout>3)&1)>4)&1)>5)&1) 关于c++-错误:invalidoperandsoftypes'int'and''tobinary'operator https://stackoverflow.com/questions/246

c++ - 为什么 std::vector::insert 需要复制赋值?

我试图理解以下行为:#include#includestructFoo{Foo(inta):a_{a}{}constinta_;//Notetheconst};intmain(intargc,char**argv){std::vectorv1{Foo{0}};std::vectorv2{Foo{1}};autofirst=std::begin(v2);autolast=std::end(v2);for(;first!=last;++first){v1.push_back(*first);//Fine}//v1.insert(v1.begin(),first,last);//Doesno

c++ - 为什么fstream不利用operator>>的istream原型(prototype)?

我有一个使用友元函数重载运算符的类>>。重载运算符方法在标准cin使用上测试得很好。但是,当我尝试将代码升级为使用ifstream对象而不是istream对象时,原型(prototype)未被识别为有效方法。我的理解是ifstream继承自istream,因此,多态性应该允许ifstream对象与istream重载函数一起操作。我的理解有什么问题吗?是否有必要为每种输入流类型复制函数?类:#include#include#includeusingnamespacestd;classHospital{public:Hospital(std::stringname);std::string

c++ - 为什么 vector::operator[] 的实现方式与 map::operator[] 不同?

std::vector的operator[]是否有任何理由只返回一个引用而不是插入一个新元素?vector::operator的cppreference.com页面显示hereUnlikestd::map::operator[],thisoperatorneverinsertsanewelementintothecontainer.map::operator[]的页面says"Returnsareferencetothevaluethatismappedtoakeyequivalenttokey,performinganinsertionifsuchkeydoesnotalreadye

c++ - 了解 cout.operator<<() 的行为

根据thisquestion的最佳答案,cout相当于cout.operator.根据对thisquestion的回复,以上说法不正确。根据我自己的测试,cout.operator与cout相同当给定一个整数时。当给出一个float时,cout.operator将其强制为整数。当给定字符串文字时,如cout.operator,它输出的似乎是一个内存地址。当给定一个包含std::string的变量时,它会给出编译器错误。谁能给出初级到中级水平的解释是怎么回事? 最佳答案 这取决于expr.这两个问题的答案都是针对特定案例的,而不是一揽

c++ - STL算法和back_inserter可以预分配空间吗?

如果我有类似的东西:vectorlongVector={...};vectornewVector;transform(longVector.begin(),longVector.end(),back_inserter(newVector),[](inti){returni*i;});STL是否能够在处理和添加新元素之前在newVector中预分配空间?我知道这不是算法的要求,但是“好的”实现能够优化它吗?或者,对于这种情况,我应该更喜欢在之前添加newVector.reserve(longVector.size());吗?我不一定要问每个stdlib实现是否有(尽管如果有人知Prop体

c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'

代码A:vector::const_reverse_iteratorrcit;vector::const_reverse_iteratortit=v.rend();for(rcit=v.rbegin();rcit!=tit;++rcit)cout代码B:vector::const_reverse_iteratorrcit;for(rcit=v.rbegin();rcit!=v.rend();++rcit)coutCODEA工作正常但是为什么代码B通过错误:DEVC++\vector_test.cpp在'rcit!=std::vector::rend()与_Tp=int,_Alloc=s

c++ - operator() 模板特化

我正在尝试对模板运算符进行特化,模板如下所示:templateResultTypeoperator()(Iterator1a,Iterator2b,size_tsize,ResultTypeworst_dist=-1)const在我做了如下所示的特化之后:templatefloatoperator()(float*a,floatconst*b,unsignedlongsize,floatworst_dist=-1)const编译时出现错误:Cannotspecializeafunction'operator()'withinclassscope所有这些函数都在结构模板中我很乐意得到一些

c++ - 为什么迭代器在 VS2010 中导致调试非常缓慢,即使 _HAS_ITERATOR_DEBUGGING、_SECURE_SCL、_SECURE_SCL_THROWS 设置为 0

我一直试图找出为什么在Debug模式下调试我们的程序需要这么长时间。在使用xperf查看堆栈的样子后,很明显我们在迭代器和STL容器上花费了大量时间。我在谷歌上搜索了一会儿,找到了选项_HAS_ITERATOR_DEBUGGING=0_SECURE_SCL=0_SECURE_SCL_THROWS=0我用#define在代码中设置所有这些#define_HAS_ITERATOR_DEBUGGING0#define_SECURE_SCL0#define_SECURE_SCL_THROWS0但这似乎没有用,所以我尝试使用visualstudio项目中的预处理器定义,但似乎仍然没有帮助。我已经