草庐IT

iteration

全部标签

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++如何在结构 vector 的一个字段上创建迭代器

我有一个所有原始类型的结构,如下所示:structrecord{intfield1;doublefield2;}我有一个该结构实例的vector,如下所示:vectorrecords;是否有可能/创建vector::iterator的最佳方法是什么?这将遍历field1?如果我使用数组recordrecords[n]会怎么样?我需要看起来像vector::iterator的东西.编辑:我需要一个vector::iterator的东西. 最佳答案 制作迭代器适配器首先,最简单的解决方案是迭代容器并从迭代器访问字段。for(auto&&

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++ - 在列表迭代期间删除元素 - 安全

我想知道这样的东西是否安全......//Iteratingthroughawhile(iter!=seq.end()){if(test){iter=seq.erase(iter);}else{++iter;}我知道以这种方式遍历vector会使迭代器失效,但同样的事情会发生在列表中吗?我假设不是,因为列表是通过指针顺序排列的,而不是在内存中彼此“相邻”,但任何保证都会有所帮助。 最佳答案 这很好,因为删除方法返回一个新的有效迭代器。 关于c++-在列表迭代期间删除元素-安全,我们在St

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++ - 为什么将十进制转换为二进制的递归方法比迭代、使用和返回字符串的方法更快?

我创建了两个接受十进制数字并返回该数字的二进制表示的函数。我选择了一种简单的方法来执行此操作,即在进行一些简单的数学运算后将1和0连接到一个字符串。我创建了一个迭代递归方法来执行此操作。然后我用老师给我的计时器课对这两种方法进行了计时。事实证明,与我的迭代方法相比,我的递归方法大约快两倍。为什么会这样?stringCConversion::decimalToBinaryIterative(intnum){stringss;while(num>0){if(num%2!=0){ss='1'+ss;}else{ss='0'+ss;}num=num/2;}returnss;}stringCCo

c++ - 在 C++ 中迭代结构

我在迭代结构时遇到了一些问题。根据编译器标志,结构可以有不同的定义。我想将所有结构成员设置为0。不知道有多少个成员,但保证都是数字(int,long...)请看下面的例子:#ifdefFLAG1structstr{inti1;longl1;doulbed1;};#elsifdefined(OPTION2)structstr{doubled1longl1;};#elsestructstr{inti1;};#endif我想我想做的一个很好的伪代码是:voidf(str*toZero){foreachmembermintoZerom=0}有没有办法在C++中轻松做到这一点?

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中没有排序.那么我怎样才能正确地做到这一点呢?我目前正在使用这种需要在每个循环中进行