草庐IT

reverse_iter

全部标签

c++ - 如何为非 const 类调用 const_iterator?

这个问题在这里已经有了答案:Howtousetwofunctions,onereturningiterator,theotherreturningconst_iterator(2个答案)关闭8年前。我阅读了一些与此问题相关的其他线程,但没有为我的问题提供解决方案。希望大家给我出出主意或建议。我正在尝试实现这个名为Map的类。它应该包含2个迭代器-iterator和const_iterator。我实现了它们-iterator继承自const_iterator,在Map类中我有以下函数:iteratorbegin();iteratorend();const_iteratorbegin()c

c++ - 当没有任何意义时, `Iterator::pointer` 使用什么?

例如,考虑一些假设的to_upper_iterator遍历一系列字符,返回std::toupper对于operator*.这些迭代器别名对我来说很有意义:templatestructto_upper_iterator{usingvalue_type=CharT;usingreference=CharT;usingdifference_type=std::ptrdiff_t;usingiterator_category=std::random_access_iterator_tag;};没有意义的是什么应该/可以用于pointer别名。我试着把它关掉,但果然我遇到了编译错误。似乎这是由于

c++ - std::map::const_iterator 泄露了对值的非常量引用?

我观察到std::map::const_iterator泄漏了对value_type的非常量引用:#include#includeintmain(intargc,char*argv[]){std::mapfoo={{1,1},{4,2}};constauto&m=foo;constauto&it=foo.find(1);printf("%d%d\n",it->first,it->second);int&i=it->second;i=3;auto&one=foo.at(1);printf("%d%d\n",1,one);return0;}输出$g++test.cc&&./a.out111

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是不