草庐IT

C++迭代器(iterator)

全部标签

c++ - 迭代器声明 : "does not contain a type"

我很难理解为什么会收到此错误。我指的是Josuttis的STL书和其他资源,看来我在下面声明我的迭代器的方式应该有效:#ifndefLRU_H#defineLRU_H#include#includeclassLRU{public:LRU();//defaultconstructorLRU(int);//constructorwithargument~LRU();//destructor//Methods//voidenqueue(int);//adddatumtothequeuevoiddequeue();//removedatumfromthequeuevoidreplace();/

c++ - 是否可以推断出 std::insert_iterator 包含的类型?

我有一个需要模板化迭代器类型的函数。它当前取消引用迭代器以检查被迭代的类型。templatevoidfunc(Iteratori){//Inspectthesizeoftheobjectsbeingiteratedconstsize_ttype_size=sizeof(*i);...}我最近发现一些标准迭代器类型,例如std::insert_iterator将*i定义为对i的简单引用.即sizeof(*i)是迭代器本身的大小;与sizeof(i)或sizeof(***i)相同是否有一种通用方法(支持C++03)来确定任何标准迭代器正在迭代的对象的大小或类型?

c++ - 如何为集合提供带有迭代器的 const 接口(interface)?

我想创建一个具有如下签名的函数://Setfoundtobeaniteratortothelocationofkeyinmaporend()//ifnotfound.boollookup(constKey&key,conststd::map&map,std::map::const_iterator&found);但我也想在映射和迭代器不是const的情况下调用它,以便我可以修改找到的值:constKeykey;std::mapmap;std::map::iteratorfound;if(lookup(key,map,found)){found->second.modifingNonCo

C ++矢量迭代器nth_element编译错误

下面的代码不会编译。在第二行的第二行有一个错误(nth_element...)。它似乎与比较器有关。编译器主张“术语不评估为2个参数的函数”。如何解决编译错误?structResult{Result(unsignedintid,doubleresult);boolcmp(constResult&a,constResult&b)const;unsignedintid;doubleresult;};Result::Result(unsignedintid,doubleresult){this->id=id;this->result=result;}boolResult::cmp(constResu

c++ - 我可以轻松地覆盖 (STL) 迭代器的类别吗?

现在,我有一个类可以通过随机访问迭代器满足API要求。但是,我可以设想这样一种情况,即实现会发生变化,只能提供一个前向迭代器。因此,我想限制调用者使用随机访问功能。我知道我可以编写自己的实现(例如restricted_bar_iterator),但想知道是否有更简单的方法(即需要更少的编码)。classBAR{...};classFOO{public://Bad...clientsmayexpect'bar_iterator'toberandomaccess...typedefstd::vector::iteratorbar_iterator;bar_iteratorbegin_bar

c++ - 为类编写双向迭代器

我有一个包含字符串的类。基本上这个类是建立在一个字符串数组上的:classstringlist{public:typedefstd::stringstr;voidpush(str);voidpop();voidprint();voidresize(size_t);size_tcapacity();size_tsize();stringlist():N(15){}stringlist(size_tsz):N(sz){}~stringlist(){delete[]container;}}private:size_tN;str*container=newstr[N];};练习的下一部分要求读

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++ - 在 C++11 "foreach"语句中是否有任何(方便的)方法来检索当前迭代#?

我想知道是否有可能以某种方式从C++11foreach语句中提取当前迭代次数。在这样的代码中:for(auto&i:vect)if(i==0)zero_value_index=/*hereIwantmyindex*/;我找不到其他方法,只能使用老式的for和inti轻松获取我的索引。想法? 最佳答案 我不知道,你可以计算迭代次数:inti=0;for(auto&el:container){if(el==0)zero_value_index=i;++i;} 关于c++-在C++11"for

c++ - 迭代器的统一初始化

我是C++11的新手,迭代器和统一初始化有问题,我不明白。考虑以下未编译的示例:#include#includeintmain(){std::vectort{1,2,3,4,5};autoiter{t.begin()};for(;iter!=t.end();++iter){std::cout在第6行中,使用统一初始化对vector进行了初始化。在第7行中,我尝试对迭代器执行相同的操作。这是行不通的。将第7行更改为autoiter=t.begin()即可。我知道我可以为此简单地使用“基于范围的”,但问题是:为什么统一初始化不适用于迭代器但适用于基本类型,例如inti{0};?

vector 中某些元素的 C++ 迭代器

如果这是一个微不足道的问题,请原谅我,我只是在学习C++并尝试围绕某些概念进行思考。尤其是涉及到迭代器时,我完全迷路了。假设我有一个表示某种数据结构的自定义类,它的成员之一是一个整数vector。我想为该类编写一个双向迭代器,它只输出vector中的偶数。有没有一种简单而有指导意义的方法?我不想使用STL以外的库。 最佳答案 不确定制作自己的迭代器是否容易。但可能最好的方法是使用有条件的for_each函数。std::for_each对每个元素进行操作。创建一个对某些特定元素执行操作的for_each_if非常容易。例如,下面的程序