草庐IT

istream_iterators

全部标签

c++ - 为什么 vector::iterator 在重新分配时无效?

我不明白为什么在重新分配发生时vector的迭代器应该失效。难道不能简单地通过在迭代器中存储一个偏移量——而不是一个指针——来防止这种情况吗?为什么vector不是这样设计的? 最佳答案 只是为与性能相关的理由添加一个引用:在设计C++时,Stroustrup认为模板类如std::vector是至关重要的。接近原生数组的性能特点:Onereasonfortheemphasisonrun-timeefficiency...wasthatIwantedtemplatestobeefficientenoughintimeandspacet

c++ - 将 std::list<>::iterator 的值指向指针?

如何循环通过STL::List并存储其中一个对象的值以供稍后在函数中使用?Particle*closestParticle;for(list::iteratorp1=mParticles.begin();p1!=mParticles.end();++p1){//ExtrastuffremovedclosestParticle=p1;//failstocompile(editfromcomments)} 最佳答案 要么Particle*closestParticle;for(list::iteratorit=mParticles.be

c++ - 我应该如何在 API 中替换 vector<uint8_t>::const_iterator?

我的任务是完善编解码器库的界面。我们使用的是C++17,我只能使用标准库(即没有Boost)。目前,有一个Decoder大致如下所示的类:classDecoder:publicCodec{public:structResult{vector::const_iteratornew_buffer_begin;optionalmetadata;optionalpacket;};Resultdecode(vector::const_iteratorbuffer_begin,vector::const_iteratorbuffer_end);private://irrelevantdetails

C++ 迭代器到 const_iterator

如何在C++中从iterator(该容器类的)获取一个const_iterator(某个容器类的)?insert_iterator中的const_iterator怎么样?生成的iterator应该指向与原始位置相同的位置。 最佳答案 容器需要提供iterator作为可转换为const_iterator的类型,因此您可以隐式转换:Container::iteratorit=/*blah*/;Container::const_iteratorcit=it;std::insert_iterators是输出迭代器。这无法将它们转换为必须是前

c++ - 有没有办法检查 istream 是否以二进制模式打开?

我正在使用一个istream,它可以是stringstream、ifstream或用户定义的流类型,我需要知道如果是ifstream,它是否没有以二进制模式打开(所以我可以抛出异常)。我试过以下方法:if((_is.flags()&ios::binary)==0)throwexception(...)但是从来没有抛出异常。在这种情况下测试失败,因为_is.flags()返回0x201而ios::binary是0x20。有没有办法确定流是否以文本模式打开? 最佳答案 flags()返回ios_base::fmtflags这是格式化标志

c++ - 为什么从 std::istream 读取记录结构字段失败,我该如何解决?

假设我们有以下情况:一个记录结构声明如下structPerson{unsignedintid;std::stringname;uint8_tage;//...};记录使用以下格式存储在文件中:IDForenameLastnameAge------------------------------1267867JohnSmith3267545JaneDoe368677453GwynethMiller5675543J.RossUnusual23...应该读入该文件以收集任意数量的上述Person记录:std::istream&ifs=std::ifstream("SampleInput.tx

c++ - iterator_trait 的典型用例是什么

我是C++新手,所以请多多包涵。我想了解STLiterator_traits.在“C++标准库”一书中,结构iterator_traits定义如下:templatestructiterator_traits{typedeftypenameT::value_typevalue_type;typedeftypenameT::difference_typedifference_type;typedeftypenameT::iterator_categoryiterator_category;typedeftypenameT::pointerpointer;typedeftypenameT::

C++ 双端队列 : when iterators are invalidated

如果我错了,请纠正我。谢谢!insert和erase将重定位元素,但插入/删除发生位置之前的元素不会重定位,因此它们的迭代器仍然有效。push_back和pop_back不会使任何迭代器失效。push_front和pop_front使所有迭代器无效。swap不会重新定位元素,但不知何故我认为它应该使迭代器无效。 最佳答案 push_back()和push_front()是根据insert()定义的。类似地,pop_back()和pop_front()是根据erase()定义的。以下是C++03标准关于insert()(23.2.1.

c++ - Visual Studio 中的 _ITERATOR_DEBUG_LEVEL 错误

我正在尝试在Windows7上的VisualStudio2010中编译JRTPLIB。这是一场真正的噩梦……但我至少缩小了问题范围。这是剩下的。Error3errorLNK2038:mismatchdetectedfor'_ITERATOR_DEBUG_LEVEL':value'2'doesn'tmatchvalue'0'inclient.objC:\Users\Johan-bar\Documents\VisualStudio2010\Projects\client\client\jrtplib.lib(rtpsession.obj)client我用谷歌搜索了很多,原因似乎是一个在De

javascript - Jade + express : Iterating over object in inline JS code (client-side)?

我想根据它的api实现一个谷歌地图。我想添加一个基于坐标的路径。因此我从我的模型中获取我的坐标,并希望遍历对象以用这些点填充map。在我的Jade模板中,我包含这样的apijs代码:script(type='text/javascript')functioninitialize(){varmyLatLng=newgoogle.maps.LatLng(0,-180);varmyOptions={zoom:3,center:myLatLng,mapTypeId:google.maps.MapTypeId.TERRAIN};varmap=newgoogle.maps.Map(document