index vs iterator [英] index vs iterator

查看:89
本文介绍了index vs iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++编程已经4年了,所以我不是新手,但我不是一个非常有经验的标准C ++库程序员。


我的问题是:


你能告诉我一个迭代器比索引好的情况吗?


对我来说(直到现在)迭代器是非常奇怪的对象,因为它们对我来说是无用的。

但是由于大多数容器成员使用迭代器,我很难过。


我认为,使用vector& map,index比迭代器更有用。

解决方案

Chameleon写道:

我用C ++编程4年,所以我不是新手,但我不是标准C ++图书馆非常有经验的程序员。

我的问题是:

你能告诉我一个吗?迭代器优于索引的情况?

对我来说(直到现在)迭代器是非常奇怪的对象,因为它们对我来说是无用的。
但是因为大多数容器成员使用迭代器,我很难过。

我认为,使用矢量& map,index比迭代器更有用。




一对迭代器指定一系列元素。获得一对

迭代器的一种方法是使用容纳实际元素的容器,但

还有其他方法。例如,通过使用两个istream_iterator< int>,读取文件的输入流可以将整数值的文本表示视为整数值的文本表示。对象。

一旦你有了这对迭代器,就可以将它们传递给标准的

算法,所以你不必重写代码,例如复制

一个序列的内容到另一个序列。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


"变色龙" < CH ****** @ hotmail.NOSPAM.com>在消息中写道

news:dr ********** @ volcano1.grnet.gr ...

:我用C ++编程4年所以我不是新手,但我不是一个

:非常有经验的标准C ++库程序员。



:我的问题是这个:



:你能告诉我一个迭代器比索引好的情况吗?



:对我来说(直到现在)迭代器是非常奇怪的对象,因为它们是

:对我没用。

:但是由于大多数容器成员使用迭代器,我是难倒。



:我认为,使用矢量& map,index比迭代器更有用。


索引只能用于(有效)支持随机访问的容器(即直接访问一个给定位置的元素。

迭代器是一个更通用的概念。迭代器提供有效遍历链接列表,文件和许多其他数据结构的
。通常

可以生成更高效的代码。


Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < ; - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com



Chameleon写道:

我用C ++编程已经4年了,所以我不是新手,但我不是一个非常有经验的程序员在标准的C ++库中。

我的问题是:

你能告诉我一个迭代器比索引好的情况吗?


第一个容易引起注意的案例是所有标准的

库算法都使用迭代器。

对我来说(直到迭代器是非常奇怪的对象,因为它们对我来说没用。
但是由于大多数容器成员使用迭代器,我很难过。

我认为,使用向量&安培; map,index比迭代器更有用。




如果你用它们做的只是读取和写入单个元素

使用operator [那么也许迭代器不会增加太多价值。但那是什么容器,迭代器和算法可以为你实现的小部分。


Gavin Deane


I am programming in C++ for 4 years, so I am not a newbie but I am not a
very experienced programmer on standard C++ libary.

My question is this:

Can you tell me one case which an iterator is better than index?

For me (until now) iterators are very strangely objects because they are
useless for me.
But since most of members of containers use iterators, I am stumped.

I think, for using vector & map, index is far more useful than iterator.

解决方案

Chameleon wrote:

I am programming in C++ for 4 years, so I am not a newbie but I am not a
very experienced programmer on standard C++ libary.

My question is this:

Can you tell me one case which an iterator is better than index?

For me (until now) iterators are very strangely objects because they are
useless for me.
But since most of members of containers use iterators, I am stumped.

I think, for using vector & map, index is far more useful than iterator.



A pair of iterators designates a sequence of elements. One way to get a
pair of iterators is to use a container that holds actual elements, but
there are other ways. For example, an input stream reading a file that
holds text representations of integer values can be treated as a
sequence of integer values by using two istream_iterator<int> objects.
Once you have that pair of iterators you can pass them to standard
algorithms, so you don''t have to rewrite the code, for example, to copy
the contents of one sequence into another.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


"Chameleon" <ch******@hotmail.NOSPAM.com> wrote in message
news:dr**********@volcano1.grnet.gr...
:I am programming in C++ for 4 years, so I am not a newbie but I am not a
: very experienced programmer on standard C++ libary.
:
: My question is this:
:
: Can you tell me one case which an iterator is better than index?
:
: For me (until now) iterators are very strangely objects because they are
: useless for me.
: But since most of members of containers use iterators, I am stumped.
:
: I think, for using vector & map, index is far more useful than iterator.

An index only can be used for containers that (efficiently) support random
access (i.e. direct access to an element at a given position).
An iterator is a more general concept. Iterators offer efficient traversal
of linked lists, files, and a number of other data structures. It often
leads to the generation of more efficient code.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



Chameleon wrote:

I am programming in C++ for 4 years, so I am not a newbie but I am not a
very experienced programmer on standard C++ libary.

My question is this:

Can you tell me one case which an iterator is better than index?
The first case that springs easily to mind is that all the standard
library algorithms use iterators.
For me (until now) iterators are very strangely objects because they are
useless for me.
But since most of members of containers use iterators, I am stumped.

I think, for using vector & map, index is far more useful than iterator.



If all you ever do with them is read and write individual elements
using operator[] then maybe iterators don''t add much value. But that''s
a small subset of what containers, iterators and algorithms can do for
you.

Gavin Deane


这篇关于index vs iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆