草庐IT

tok_iter

全部标签

c++ - 如果容器元素是指针,为什么允许我从 const_iterator 调用非 const 成员函数?

考虑以下代码:#includeusingnamespacestd;structfoo{voidbar(){}};intmain(){{vectora;a.push_back(newfoo());a.push_back(newfoo());a.push_back(newfoo());vector::const_iteratoritr=a.begin();(*itr)->bar();//compiles-thisbecomesmoreconfusing//whenfoundinaconstmethod.Onfirst//glance,onewill(oratleastme)may//ass

c++ - 为什么 istream_iterator<string>(ifstream ("test.txt")) 会导致错误?

我尝试编写代码从名为“test.txt”的文件中读取字符串并将字符串写入标准输出。下面的代码运行良好:intmain(){usingnamespacestd;ifstreamfile("test.txt");copy(istream_iterator(file),istream_iterator(),ostream_iterator(cout,""));}但是,通过此修改,代码不再编译:intmain(){usingnamespacestd;copy(istream_iterator(ifstream("test.txt")),//(),ostream_iterator(cout,""

C++ : double iteration through map

我想遍历一个map,但内部循环只会遍历元素的上半部分。使用vector它看起来像这样:for(autoelement1=myVector.begin();element1!=myVector.end();++element1){for(autoelement2=element1+1;element2!=myVector.end();++element2){//mystuff}}但是对于mapelement1+1返回错误nooperatormatchesthisoperand..我相信这是因为元素在map中没有排序.那么我怎样才能正确地做到这一点呢?我目前正在使用这种需要在每个循环中进行

c++ - 使用带有 split_iterator 的分类器

关于boost字符串算法,我似乎遗漏了一些东西。我正在尝试将split_iterator与使用Clasifier作为拆分点一起使用。所以,例如,我希望能够做这样的事情:make_split_iterator(str,is_space);但是make_split_iterator需要一个Range和一个Finder。所以我需要的是找到一些序列来从分类器创建查找器。有谁知道如何做到这一点,或者甚至可能吗? 最佳答案 您可以使用token_finder,如make_split_iterator(str,token_finder(is_sp

c++ - istream_iterator cin 初始化等待输入

我有这个代码片段。istream_iterator对象仅被定义而未被使用,因此我预计它不会执行任何操作并且应用程序会立即完成。但是当我运行应用程序时,在我提供一些输入之前它不会完成。为什么?我在ArchLinux上编译它:gcc4.7.1,命令:g++-std=c++11filename.cpp#include#includeusingnamespacestd;intmain(intargc,char*argv[]){istream_iteratorinput(cin);return0;} 最佳答案 按照标准,24.6.1.1ist

c++ - 为什么 boost filter_iterator 有奇怪的 make_filter_iterator 函数?

在经历了一些痛苦之后,我设法拼凑了这个boostfilter_iterator的最小示例usingnamespacestd;std::functionstlfunc=[](uint32_tn){returnn%3==0;};intmain(){vectornumbers{11,22,33,44,55,66,77,3,6,9};autostart=boost::make_filter_iterator(stlfunc,numbers.begin(),numbers.end());autoend=boost::make_filter_iterator(stlfunc,numbers.end

c++ - map<T,T>::iterator 作为参数类型

我有一个带有私有(private)映射成员的模板类templateclassMyClass{public:MyClass(){}private:std::mapmyMap;}我想创建一个接受映射迭代器的私有(private)方法voidMyFunction(std::map::iterator&myIter){....}但是,这会出现编译错误:标识符“迭代器”。我不需要传递一个抽象迭代器,因为MyFunction知道它是一个映射迭代器(并且只会用作myMap的交互器)并且会这样对待它(访问和修改myIter->second)。将myIter->second传递给MyFunction是不

c++ - "vector iterator + offset out of range"断言有用吗?

这个完美的程序在VisualStudio2013的Debug模式下失败:#include#include#includeusingnamespacestd;voidmain(){vectorv={3,1,4,1,5,9,2,6,5,3};for(autoiFrom=v.cbegin(),iTo=iFrom+5;iFrom!=v.cend();iFrom=iTo,iTo+=5)coutvectoriterator+offsetoutofrange断言失败。它失败是因为iTo>v.cend(),这在这里是无害的。调试器测试没有被取消引用的迭代器的值有什么意义?顺便说一句,我知道我可以将上面

c++ - 自定义双向迭代器的 reverse_iterator 上的 for_each 需要 OutputIterator

我创建了一个简单的不可变双向迭代器:#include#include#include#include#includeclassmy_iterator:publicstd::iterator{intd_val;public:my_iterator():d_val(0){}my_iterator(intval):d_val(val){}my_iteratoroperator--(int){d_val--;returnmy_iterator(d_val+1);}my_iterator&operator--(){d_val--;return*this;}my_iteratoroperator+

c++ - Vector.erase(Iterator) 导致错误的内存访问

我正在尝试对存储在vector中的videoObjects进行Z-Index重新排序。计划是识别将要放在vector第一个位置的videoObject,将其删除,然后将其插入到第一个位置。不幸的是,erase()函数总是导致错误的内存访问。这是我的代码:测试应用.h:vectorvideoObjects;vector::iteratoritVid;测试应用.cpp://GetthevideoObjectwhichrelatestotheusereventfor(itVid=videoObjects.begin();itVid!=videoObjects.end();++itVid){i