草庐IT

c++ - C++ 编译器可以假定 const bool & value 不会改变吗?

C++编译器能否假定“constbool&”值不会改变?例如,假设我有一个类:classtest{public:test(constbool&state):_test(state){}voiddoSomething(){if(_test){doMore();}}voiddoMore();private:constbool&_test;};我按如下方式使用它:voidexample(){boolmyState=true;testmyTest(myState);while(someTest()){myTest.doSomething();myState=anotherTest();}}标准

c++ - 是否可以将一对 <Key, Value> 转换为一对 <const Key, Value>?

所以我有一个模拟映射const_iterator的智能迭代器,它需要在内部构建返回类型。显然,我想存储一个pair在我的迭代器类中(因为我需要修改它),但同时我希望解引用函数呈现一个pair(实际上它分别是constpair&和constpair*)。到目前为止我想出的唯一解决方案是每次更改我的迭代器类指向更改的值时动态分配一对新的。不用说,这不是一个好的解决方案。我也试过*const_cast>(&value)其中value声明为pair.我们将不胜感激任何帮助(以及无法完成的知识)。编辑出于好奇:我最终存储了一个pairp在我的迭代器类中。为了更改这对,我根据底层迭代器(map::

C++ operator= 返回对 *this 的引用

看下面的代码:#includeusingnamespacestd;classWidet{public:Widet(intval=0):value(val){}Widet&operator=(Widet&rhs){value=rhs.value;return*this;}intgetValue(){returnvalue;}private:intvalue;};intmain(){Widetobj1(1);Widetobj2(2);Widetobj3(0);(obj3=obj2)=obj1;cout代码运行成功,输出为(使用VS2008):当我让operator=返回一个值而不是引用时:

C++ 忽略构造函数异常的后果

我已经搜索了SO来寻找这个问题的答案,但没有找到。当一个对象在构造函数的末尾抛出异常时,该对象是否有效,或者这是“取决于构造技术”的对象之一?例子:structFraction{intm_numerator;intm_denominator;Fraction(doublevalue,intdenominator);};Fraction::Fraction(doublevalue,intdenominator):m_numerator(0),m_denominator(denominator){if(denominator==0){/*E1*/throwstd::logic_error(

c++ - 根据 value_type 调用适当的构造函数 : integer or float

我有一个函数,它使用均匀分布将最小值和最大值之间的随机值填充到容器中。#include#include#include#includetemplatevoiduniform_random(TContainer&container,consttypenameTContainer::value_typemin,consttypenameTContainer::value_typemax){std::random_devicerd;std::mt19937gen(rd());//Belowlinedoesnotworkwithintegerscontainerstd::uniform_rea

c++ - 如何在 while 循环中计算 "std::cin>>value"?

目前正在自学C++Primer5th。这里有一些我不确定的东西。(我在F.A.Q上找不到确切的相关问题)。考虑这个while循环:while(std::cin>>value){...}\\valueherewasdefinedasint.课本上说:Thatexpressionreadsthenextnumberfromthestandardinputandstoresthatnumberinvalue.Theinputoperator(§1.2,p.8)returnsitsleftoperand,whichinthiscaseisstd::cin.Thiscondition,there

c++ - 错误 C2106 : '=' : left operand must be l-value

查看有关错误C2106的其他问题,我仍然不知道我的代码有什么问题。编译时出现以下错误:c:\driver.cpp(99):errorC2106:'=':leftoperandmustbel-valuec:\driver.cpp(169):errorC2106:'=':leftoperandmustbel-value代码行如下:payroll.at(i)=NULL;//Line99payroll.at(count++)=ePtr;//Line169我不明白为什么会抛出这个错误。在这个项目中,我将我的driver.cpp从员工对象指针数组更改为我制作的自定义Vector模板。我声明Vect

c++ - 使用无分配交换有什么明显的缺点吗?

我正在实现(出于培训目的)冒泡排序模板函数:templatevoidBubbleSort(iterInputfirst1,iterInputlast1,predicatefunc){boolswapped(false);do{swapped=false;iterInputbegin=first1;iterInputbeginMinus=first1;++begin;for(;begin!=last1;begin++,beginMinus++){if(func(*beginMinus,*begin)){std::swap(*beginMinus,*begin);swapped=true;

c++ - 如何正确初始化一个对象。 [C++]

我在之前的一个问题中提到,我正在阅读HerbSutter和AndreiAlexandrescu合着的《C++编码标准》一书。在其中一章中,他们是这样说的:Alwaysperformunmanagedresourceacquisition,suchasanewexpressionwhoseresultisnotimmediatelypassedtoasmartpointerconstructor,intheconstructorbodyandnotininitializerlists.这是否意味着我应该使用这种形式的构造(前提是data_3_必须用new初始化):SomeClass(co

c++ - 代码优化;切换与 if 的

我有一个问题,关于在一个经常被调用的函数中是使用“case”还是“ifs”。这是现在的以下内容,在“ifs”中;代码是不言自明的:intidentifyMsg(char*textbuff){if(!strcmp(textbuff,"text")){return1;}if(!strcmp(textbuff,"name")){return2;}if(!strcmp(textbuff,"list")){return3;}if(!strcmp(textbuff,"remv")){return4;}if(!strcmp(textbuff,"ipad")){return5;}if(!strcmp(