关于迭代器有关疑问 100分

mm542208 2005-05-29 08:57:40
请问:

什么叫迭代?

迭代叫什么?

迭代器是什么?

可以什么都说不清,请给我说清什么叫:迭代
...全文
145 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
donghui0714 2010-06-02
  • 打赏
  • 举报
回复
迭代器是一个“可遍历STL容器内全部或部分元素”的对象。一个迭代器用来指出容器中的一个特定位置。基本操作如下:
Operator *
返回当前位置的上的元素。如果该元素拥有成员,则可通过迭代器,直接以operator-> 取用他们。
Operator ++
将迭代器前进至下一个元素。
Operator == 和Operator!=
判断两个迭代器是否指向同一位置。

所有容器类别都提供一些成员函数,使我们得以获得迭代器并以之遍访所有的元素,这些函数中最重要的是:

begin()
返回一个迭代器,指向容器起始点,也就是第一个元素的位置。
end()
返回一个迭代器,指向容器结束点。结束点在最后一个元素之后(不是最后一个元素).

于是,begin()和end()形成了一个半开区间。
我们开看一下如下的代码

#include <iostream>
#include <list>

using namespace std;

int main()
{
list<char> coll;

for (char c = 'a';c <= 'z';++c)
coll.push_back(c);

list<char>::const_iterator pos;

for (pos = coll.begin();pos != coll.end();++pos)
{
cout << *pos << ' ';
}
system("pause");
}

代码分析:
迭代器pos声明在循环之前,其型别是“指向容器中的不可变元素”的迭代器。

任何一种容器都定义有两种迭代器型别:
1 container::iterator
这种迭代器以“读/写”模式遍历元素。
如果你采用iterator迭代器,而且元素本身的型别是非常量,那么就可以透过迭代器更改元素值。例如:
list<char>::iterator pos;
for (pos = coll.begin();pos != coll.end();++pos)
*pos = toupper(*pos);
注意:这里使用“前置式递增”++pos,因为它比“后置式递增”pos++效率高。后者需要一个额外的临时对象,
所以一般情况下最好使用++pos。(虽然说对程序的执行没有太大的影响,但是养成这种变成习惯还是比较好的)
2 container::const_iterator
这种迭代器以“只读”模式遍历元素。
useresu 2005-05-31
  • 打赏
  • 举报
回复
这是Alexander Stepanov和Meng Lee的解释
useresu 2005-05-31
  • 打赏
  • 举报
回复
Iterators are a generalization of pointers that allow a programmer to work with different data structures
(containers) in a uniform manner. To be able to construct template algorithms that work correctly and
efficiently on different types of data structures, we need to formalize not just the interfaces but also the
semantics and complexity assumptions of iterators. Iterators are objects that have operator* returning a
value of some class or built-in type T called a value type of the iterator. For every iterator type X for which
equality is defined, there is a corresponding signed integral type called the distance type of the iterator.
Since iterators are a generalization of pointers, their semantics is a generalization of the semantics of
pointers in C++. This assures that every template function that takes iterators works with regular pointers.
Depending on the operations defined on them, there are five categories of iterators: input iterators, output
iterators, forward iterators, bidirectional iterators and random access iterators. Forward iterators satisfy all the
requirements of the input and output iterators and can be used whenever either kind is specified.
Bidirectional iterators satisfy all the requirements of the forward iterators and can be used whenever a
forward iterator is specified. Random access iterators satisfy all the requirements of bidirectional iterators
and can be used whenever a bidirectional iterator is specified. There is an additional attribute that forward,
bidirectional and random access iterators might have, that is, they can be mutable or constant depending on
whether the result of the operator* behaves as a reference or as a reference to a constant. Constant iterators
do not satisfy the requirements for output iterators.
nasi00 2005-05-31
  • 打赏
  • 举报
回复
迭代和迭代器两码事

迭代是数学上的,比如把计算结果带回去做变量继续算,就是迭代了

迭代器是STL里面的,就是提供一种访问容器内部成员的方法
qhfu 2005-05-29
  • 打赏
  • 举报
回复
overlap
chenzhixin 2005-05-29
  • 打赏
  • 举报
回复
C++中的迭代我不清楚,我给你个数学上的定义,你自己参考参考下,也许有点用


引用数值方法中迭代的定义:

迭代是指重复执行一个计算过程,只到找到答案


首先需要一个用于逐渐计算的规则或函数g(x),和一个起始点P0.然后通过迭代规则
Pk+1=g(Pk),得到序列{Pk}
foochow 2005-05-29
  • 打赏
  • 举报
回复
对于STL数据结构和算法,你可以使用五种迭代器。
· Input iterators 提供对数据的只读访问。

· Output iterators 提供对数据的只写访问

· Forward iterators 提供读写操作,并能向前推进迭代器。

· Bidirectional iterators提供读写操作,并能向前和向后操作。

· Random access iterators提供读写操作,并能在数据中随机移动。


foochow 2005-05-29
  • 打赏
  • 举报
回复
迭代器提供了访问容器中对象的方法。例如,可以使用一对迭代器指定list或vector中的一定范围的对象。迭代器就如同一个指针。事实上,C++的指针也是一种迭代器。但是,迭代器也可以是那些定义了operator*()以及其他类似于指针的操作符地方法的类对象
qhfu 2005-05-29
  • 打赏
  • 举报
回复
迭代就是一个一个访问容器中的元素,, 迭代器可以前向迭代器,也可以后向迭代器,也有双向迭代器,也有随机访问迭代器。 抽象出来做为容器和算法的接口。

stl的三要素就是 容器,迭代器,算法,, 其中迭代器就是做为两者的接口,这样 容器和算法就可以各自设计,只要符合接口规范,

不过我觉得理解这种东西就像理解面向对象一样,, 接触多了,,自然就知道了,,
CMyMfc 2005-05-29
  • 打赏
  • 举报
回复
迭代可以理解为,用它可以访问一个容器的所有元素
CMyMfc 2005-05-29
  • 打赏
  • 举报
回复
把它当指针看就行了

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧