std :: container :: iterator vs std :: container :: pointer [英] std::container::iterator vs std::container::pointer

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

问题描述

大家好,


我试过在sgi文档中查找这个内容但它没有提供任何具体的

回答我的''我正在寻找。基本上,

使用:: iterator用于容器与使用::指针之间有什么区别吗?


我做了一个快速实验并替换了所有: :我的代码中的迭代器与

::指针,它似乎工作相同。表面上我想的是

他们是一样的但也许在我不知道的

表面下面有一些细微的差别的?


谢谢

Hi all,

I tried looking this up in the sgi docs but it didn''t provide any concrete
answer to what I''m looking for. Basically, is there any difference between
using ::iterator for a container vs using ::pointer?

I did a quick experiment and replaced all the ::iterator in my code with
::pointer and it seems to work the same. What I''m think is on the surface
they''re the same but perhaps there''re some subtle differences beneath the
surface that I''m unaware of?

Thanks

推荐答案



" Vivi Orunitia" <六** @ blackmagevillage.com>在消息中写道

news:Xn ********************************** @ 199.45 .4 9.11 ...

"Vivi Orunitia" <Vi**@blackmagevillage.com> wrote in message
news:Xn**********************************@199.45.4 9.11...
大家好,

我试过在sgi文档中查找这个但是它没有提供任何具体的答案我在找。基本上,对于容器和使用::指针使用:: iterator之间是否有任何区别?

我做了一个快速实验并用 ::指针,它似乎工作相同。表面上我想的是他们是一样的但也许在我不知道的表面下面有一些细微的差别?
Hi all,

I tried looking this up in the sgi docs but it didn''t provide any concrete
answer to what I''m looking for. Basically, is there any difference between
using ::iterator for a container vs using ::pointer?

I did a quick experiment and replaced all the ::iterator in my code with
::pointer and it seems to work the same. What I''m think is on the surface
they''re the same but perhaps there''re some subtle differences beneath the
surface that I''m unaware of?




是的,有区别。

对于STL容器Cont< T,..>

以下typedef''提供


typedef T value_type;

typedef T *指针;

typedef T&参考;


迭代器可能是其他类或容器::指针。

实际上任何给出迭代器概念的东西(类/指针)都可以是容器的
迭代器。

所以不能假设container :: pointer和container :: iterator

是实际上是一样的。


祝福,

Sharad



Yes there is a difference.
For an STL container Cont<T,..>
the following typedef''s are provided

typedef T value_type;
typedef T* pointer;
typedef T& reference;

Iterator could be some other class or container::pointer.
In fact anything(class/pointer) that gives the notion of an iterator could be an
iterator to the container.
So one can''t make assumptions that container::pointer and container::iterator
are actually same.

Best wishes,
Sharad


据我所知告诉迭代器具有指针的所有操作,例如

deferencing *, - >等,这是有意义的,因为迭代器被认为是指针的概括。


但是,我可以想象,迭代器的某些操作可能不会像++那样实现
,等等。如果它们是特定的迭代器

类别如前向迭代器,反向迭代器等。


" Sharad Kala" <无***************** @ yahoo.com>写在

新闻:bv ************ @ ID-221354.news.uni-berlin.de:
As far as I can tell an iterator has all the operations of a pointer like
deferencing *, ->, etc. which makes sense as iterators are suppose to be
generalizations of pointers.

I can imagine, however, where some operations of an iterator might not be
implemented like ++, -- etc. if they''re an iterator of a particular
category like forward iterators, reverse iterators etc.

"Sharad Kala" <no*****************@yahoo.com> wrote in
news:bv************@ID-221354.news.uni-berlin.de:


Iterator可能是其他类或容器::指针。
实际上任何给出迭代器概念的东西(类/指针)都可以是容器的迭代器。
所以不能假设container :: pointer和
container :: iterator实际上是相同的。


Iterator could be some other class or container::pointer.
In fact anything(class/pointer) that gives the notion of an iterator
could be an iterator to the container.
So one can''t make assumptions that container::pointer and
container::iterator are actually same.



$ b $嗯,所以就使用它们而言,在什么情况下这个差异很重要?例如,使用:: iterator

和:: pointer可互换会导致潜在的问题等。


感谢您的回复:)



hmm, so as far as using them is concerned under what situations would
this difference be important? Like for example, where using ::iterator
and ::pointer interchangably would cause potential problems etc.

Thanks for the response :)


2004年2月3日星期二08:46:58 +0000,Vivi Orunitia写道:
On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:
Iterator可能是其他类或容器::指针。
实际上任何给出迭代器概念的东西(类/指针)都可以是容器的迭代器。
所以可以'假设container :: pointer和
container :: iterator实际上是相同的。
Iterator could be some other class or container::pointer.
In fact anything(class/pointer) that gives the notion of an iterator
could be an iterator to the container.
So one can''t make assumptions that container::pointer and
container::iterator are actually same.



嗯,所以就使用它们而言情况会不会这种差异很重要?例如,使用:: iterator
和:: pointer可互换会导致潜在问题等。



hmm, so as far as using them is concerned under what situations would
this difference be important? Like for example, where using ::iterator
and ::pointer interchangably would cause potential problems etc.




取std :: list。一个迭代器肯定与指针不同。

它的operator ++必须知道如何追逐链表,这是一个非常不同的b / b
操作,只是递增一个指针。 std :: map :: iterator也有

指向下一个元素,它通过单步执行树,

也是一个非常不同的操作来增加一个指针。


但一般来说迭代器是_is_not_指针。有时迭代器可能实现为指针,但我想这对于std :: vector

无论如何都是可能的。即使对于std :: vector,实现也允许使用一些

类而不是指针,实际上有些类似。


无论如何,迭代器是建模的在指针之后,它为大多数程序员提供了一个非常熟悉的方便的

框架。然而,这个建模

是概念级别的(你可以取消引用它,增加它等),

不在实现级别上。这就是迭代器之美,你可以将它增加到指向下一个元素,而不必担心这个迭代器需要做的所有魔法的
找到下一个元素。


HTH,

M4



Take std::list. An iterator there is definately different from a pointer.
Its operator++ has to know how to chase the linked list, a very different
operation for just incrementing a pointer. A std::map::iterator also has
to point at the next element, it does so by stepping through the tree,
also a very different operation from incrementing a pointer.

But in general an iterator _is_not_ a pointer. Sometimes iterators may be
implemented as pointers, but I guess this is only possible for std::vector
anyhow. Even for std::vector, the implementation is allowed to use some
class instead of pointers, and indeed some do.

Anyhow, an iterator is modeled after a pointer, it makes for a convenient
framework that is very familiar to most programmers. However this modeling
is on the conceptual level (you can dereference it, increment it, etc),
not on the implementation level. That is the beauty of iterators, you can
just increment it to point at the next element, without having to worry
about all the magic that the iterator needs to do to find the next element.

HTH,
M4


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

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