草庐IT

const_iterators

全部标签

c++ - 可能返回值也可能不返回值的函数的 Iterator-reducer 模式

以下函数在每个元素上应用仿函数并减少返回值:templateRED::TYPEforAllElements(FCTfunctor,REDreducer){for(/*alleleminelements*/){reducer(functor(elem));}returnreducer.value;}现在,有时我可能希望只对所有元素调用functor,而不减少任何东西。基本上,我想要这样的东西:classFunctorThatReturnsNothing{voidoperator()(Eleme){//dosomething,returnnothing...}}classDummyRedu

c++ - 二进制表达式 ('const' 和 'const' 的无效操作数)

这个问题在这里已经有了答案:HowcanIusestd::mapswithuser-definedtypesaskey?(8个答案)关闭6年前。我是一个C++菜鸟,所以如果你觉得这个问题很傻,请不要介意我在下面用C++声明map:std::map>>radioMap;完整代码:不知道,但使用下面的代码,我可以解决我的问题classRadioMap:publicstd::iterator_traits,publicCloneable{private:std::map>*>radioMap;std::vectorradioDevices;public:voidadd(constCartes

c++ - 采用 const 参数的默认移动构造函数

定义类时,以下是否有效?T(constT&&)=default;我正在阅读移动构造函数here并且它解释了如何仍然可以隐式声明默认值:Aclasscanhavemultiplemoveconstructors,e.g.bothT::T(constT&&)andT::T(T&&).Ifsomeuser-definedmoveconstructorsarepresent,theusermaystillforcethegenerationoftheimplicitlydeclaredmoveconstructorwiththekeyworddefault.在页面底部,它提到了缺陷报告CWG2

C++如何将ifstream内容(来自文件)分配给具有偏移量的字符串(istreambuf_iterator)

我尝试将二进制文件内容的一部分读入字符串。为什么是字符串?我的消息协议(protocol)(使用protobuf)需要这个。下面的效果很好:std::string*data=newstd::string();std::ifstreamifs("C:\\data.bin",std::ios::binary);data->assign((std::istreambuf_iterator(ifs)),(std::istreambuf_iterator()));但这是为了从头到尾读取文件。我只想阅读给定位置的部分。例如从位置字节10开始:std::string*data=newstd::str

c++ - 在抛出 'char const*' 错误实例后调用终止

我是编程新手,当我尝试使用异常处理时,我在代码块16:01中遇到错误在抛出'charconst*'实例后调用终止这是错误。谁能帮我解决这个错误,我尝试将IDE重置为默认值,但没有成功代码是#include#include#includeusingnamespacestd;doublesqrt(doublenum){if(num>x;doublenum;try{num=sqrt(x);}catch(constchar*text){cout 最佳答案 无论导致错误的实现细节如何,您的程序都有未定义的行为,因为您使用的是C库中的保留函数签

c++ - 使用可能抛出的表达式初始化 const 变量的推荐方法

您可能知道这样的情况,您只想将一个(const)变量赋值给一个可能会失败(抛出)的表达式(例如container.at())这会迫使您编写样板代码:voidfoo(conststring&key){autoit=data_store.find(key);if(it==data_store.end()){return;}constauto&element=it->second;...goonwith`element`......}在Python中你可以这样写代码:deffoo(name):try:element=data_store[key]exceptKeyError:return..

c++ - 通过 const 引用获取字符串

这个问题在这里已经有了答案:KeepconstantreferencetoreturnvalueoffunctioninC++(3个答案)关闭4年前。std::stringf(){return"xx";}intmain(){conststd::string&ref=f();//useref}f按值返回临时字符串。main通过const引用“捕获”它。在C++中可以吗?

c++ - const 返回类型何时会干扰模板实例化?

来自HerbSutter的GotW#6Return-by-valueshouldnormallybeconstfornon-builtinreturntypes....Note:Lakos(pg.618)arguesagainstreturningconstvalue,andnotesthatitisredundantforbuiltinsanyway(forexample,returning"constint"),whichhenotesmayinterferewithtemplateinstantiation.虽然Sutter在Lakos按值返回非构建类型的对象时,似乎不同意返回c

c++ - 具有 const 成员的构造函数和匿名 union

是否可以与const成员建立匿名union?我有以下内容:structBar{union{struct{constintx,y;};constintxy[2];};Bar():x(1),y(2){}};使用G++4.5我得到错误:error:uninitializedmember‘Bar::::xy’with‘const’type‘constint[2]’ 最佳答案 这是GCC中的一个问题,已在4.6版中修复。您的代码现在可以正常工作。它仍然依赖于GCC扩展,因为它使用匿名结构,但现在大多数编译器都支持它们。此外,以下代码现在可以使

c++ - 为什么 std::set<K, C, A>::erase 不采用 const_iterator?

看来,根据ISO148822003(又名C++的神圣标准)std::set::erase需要iterator作为参数(不是const_iterator)from23.3.3[2]voiderase(iteratorposition);可能还值得注意的是,在我的VS2008附带的STL实现中,删除需要一个const_iterator。当我试图用另一个编译器编译我的代码时,这导致了一个不愉快的惊喜。现在,因为我的版本需要const_iterator,然后可以用const_iterator实现删除(好像这不是不言而喻的)。我想标准委员会已经考虑了一些实现(或手头现有的实现),这需要删除才能采