由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ interview: iterator 和 pointer区别?
相关主题
c++ pointers are iterators, why?Pointer to iterator?
stl的interator是一种smart pointer吗请教 C++ std::list iterator 对 template class pointer 的应用问题
C++ 菜鸟问一个关于template 的问题。c++ 里用到pointer 的地方我们尽可能用smart pointer吗?
deque的pointer和reference是怎么回事?弱问c++ iterator 和 pointer区别
关于不同类型的smart pointer树的前序遍历
请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)一个C#使用C++.NET类库的问题
is smart_ptr really that good?请问delete的问题
Use std::swap to exchange pointers and iterators大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.
相关话题的讨论汇总
话题: iterator话题: pointer话题: smart话题: c++话题: stl
进入Programming版参与讨论
1 (共1页)
h****b
发帖数: 157
1
是不是map:iterator可以指向下一个元素,所以,用iterator自动traverse tree,因为
map是
sorted.
还有什么区别?
z****e
发帖数: 2024
2
iterator is a smart pointer.
all thing related is about smart pointer vs naked pointer.
such like:
1.resource management
2.exception safety
3.reference counter
etc,
smart pointer has cp-ctor and op= overloaded.
but using smart pointer, usually you lose the usual pointer arithmetic.
to calculate smart pointer difference, you need to call distance(first, last).
nothing comes at no cost, smart pointer is usually more space consuming and time consuming than regular pointer. But being a coder, you ge
X****r
发帖数: 3557
3
That is not the right answer.
In C++, Iterator is a concept. A concept is a set of requirement for
a type. For example, a random access iterator must allow operators
++, +, +=, --, -, -=, [], etc. with the right semantics, while a
forward iterator only needs to have ++. Naked pointers can be
iterators, because they already have these operators built-in.
User-defined types can also be iterators, as long as they overload
required operators correctly.
So the difference between iterator and pointer

【在 z****e 的大作中提到】
: iterator is a smart pointer.
: all thing related is about smart pointer vs naked pointer.
: such like:
: 1.resource management
: 2.exception safety
: 3.reference counter
: etc,
: smart pointer has cp-ctor and op= overloaded.
: but using smart pointer, usually you lose the usual pointer arithmetic.
: to calculate smart pointer difference, you need to call distance(first, last).

g***8
发帖数: 27
4
iterator is a constructed object which behave like a pointer
z****e
发帖数: 2024
5
你说得那些我一句话就搞定了啊:
。。。usually you lose the pointer arithmetic(这个就包括你说的那些个运算符
重载了)。其他都是pointer arithmetic啊。
STL里边的iterator分类也不一定就必须是所有iterator的分类啊。
iterator不也是一种design pattern 么?
而且iterator 就是一种smart pointer 啊,这个也没错啊。
其实我也没答好,本来想多说一些,却说得有点过了,该详细说说的地方没有展开。
但是我没有什么原则性的错误啊,这些理解都是对的啊。
你说是吧。

【在 X****r 的大作中提到】
: That is not the right answer.
: In C++, Iterator is a concept. A concept is a set of requirement for
: a type. For example, a random access iterator must allow operators
: ++, +, +=, --, -, -=, [], etc. with the right semantics, while a
: forward iterator only needs to have ++. Naked pointers can be
: iterators, because they already have these operators built-in.
: User-defined types can also be iterators, as long as they overload
: required operators correctly.
: So the difference between iterator and pointer

X****r
发帖数: 3557
6
你这个是原则性的错误。iterator既可以是smart pointer,也可以是naked
pointer,甚至可以是不smart也不naked的pointer,因为iterator并不要求
管理资源。iterator和smart pointer根本就是两码事。iterator未必没有
pointer arithmetic,比如我刚才说的random access iterator就完全
具有所有的pointer arithmetic。

【在 z****e 的大作中提到】
: 你说得那些我一句话就搞定了啊:
: 。。。usually you lose the pointer arithmetic(这个就包括你说的那些个运算符
: 重载了)。其他都是pointer arithmetic啊。
: STL里边的iterator分类也不一定就必须是所有iterator的分类啊。
: iterator不也是一种design pattern 么?
: 而且iterator 就是一种smart pointer 啊,这个也没错啊。
: 其实我也没答好,本来想多说一些,却说得有点过了,该详细说说的地方没有展开。
: 但是我没有什么原则性的错误啊,这些理解都是对的啊。
: 你说是吧。

z****e
发帖数: 2024
7
我没说iterator 绝对没有pointer arithmetic啊,要就自己overload。但是比如STL,
除了random access类型的,其他运算符都不全,所以我说"usually"也没说错啊。
iterator 不要求管理资源不代表不能管理资源啊。所以我也没说错啊。iterator本身
就是一种design pattern啊,你想让它作甚,就作甚啊。
iterator是smart pointer,我也没说错啊。关于什么是精确的smart pointer,不是现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?
如果说iterator是naked pointer,没错,但是这个题目就没什么可答的了。
我还是觉得,我可能语言没有整理好,就是条理性差些,侧重点答的不好,
但是没有原则性错误。
以上你逐条批判的话,我也都防住了。

【在 X****r 的大作中提到】
: 你这个是原则性的错误。iterator既可以是smart pointer,也可以是naked
: pointer,甚至可以是不smart也不naked的pointer,因为iterator并不要求
: 管理资源。iterator和smart pointer根本就是两码事。iterator未必没有
: pointer arithmetic,比如我刚才说的random access iterator就完全
: 具有所有的pointer arithmetic。

t****t
发帖数: 6806
8
你这个答得就叫做答非所问,比如说问粮食和米有什么区别,别人期望的答案是米是一
种粮食。你答什么呢?你答粮食有时候可以吃,有时候可以拿来烧着玩。理论上是对的
,但是不是别人期望的答案。

【在 z****e 的大作中提到】
: 我没说iterator 绝对没有pointer arithmetic啊,要就自己overload。但是比如STL,
: 除了random access类型的,其他运算符都不全,所以我说"usually"也没说错啊。
: iterator 不要求管理资源不代表不能管理资源啊。所以我也没说错啊。iterator本身
: 就是一种design pattern啊,你想让它作甚,就作甚啊。
: iterator是smart pointer,我也没说错啊。关于什么是精确的smart pointer,不是现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?
: 如果说iterator是naked pointer,没错,但是这个题目就没什么可答的了。
: 我还是觉得,我可能语言没有整理好,就是条理性差些,侧重点答的不好,
: 但是没有原则性错误。
: 以上你逐条批判的话,我也都防住了。

z****e
发帖数: 2024
9
我靠,两位恩师都来了!
我错了!
思过崖面壁去了。
t****t
发帖数: 6806
10
等等,你这答案根本不对。iterator is not smart pointer. at all.
which iterator is smart pointer (that can manage resource) in STL? none.
and design pattern is not "想让它作甚,就作甚". in addition, the design patt
ern iterator is not what asked here.
recommended book: effective STL...

现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?

【在 z****e 的大作中提到】
: 我没说iterator 绝对没有pointer arithmetic啊,要就自己overload。但是比如STL,
: 除了random access类型的,其他运算符都不全,所以我说"usually"也没说错啊。
: iterator 不要求管理资源不代表不能管理资源啊。所以我也没说错啊。iterator本身
: 就是一种design pattern啊,你想让它作甚,就作甚啊。
: iterator是smart pointer,我也没说错啊。关于什么是精确的smart pointer,不是现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?
: 如果说iterator是naked pointer,没错,但是这个题目就没什么可答的了。
: 我还是觉得,我可能语言没有整理好,就是条理性差些,侧重点答的不好,
: 但是没有原则性错误。
: 以上你逐条批判的话,我也都防住了。

相关主题
请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)Pointer to iterator?
is smart_ptr really that good?请教 C++ std::list iterator 对 template class pointer 的应用问题
Use std::swap to exchange pointers and iteratorsc++ 里用到pointer 的地方我们尽可能用smart pointer吗?
进入Programming版参与讨论
z****e
发帖数: 2024
11
我说过了,iterator 是 smart pointer 这个不是我先发明的,是书上说的,好几本都
出现过这种说法。
这是一个说法,至于什么才是 “smart pointer ”,就是文字游戏和标注之争了。
我完全同意iterator在STL里边不能resource management.但是不带表不能设计一种具有此工能iterator啊。
我思过去了,不敢和二位恩师争。
我错了!

patt

【在 t****t 的大作中提到】
: 等等,你这答案根本不对。iterator is not smart pointer. at all.
: which iterator is smart pointer (that can manage resource) in STL? none.
: and design pattern is not "想让它作甚,就作甚". in addition, the design patt
: ern iterator is not what asked here.
: recommended book: effective STL...
:
: 现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?

X****r
发帖数: 3557
12
除非所有的iterator都是smart pointer,你说iterator是smart pointer
就是错误的。某些iterator是smart pointer不改变这一点。

现在还
有标准的争论么? 我这里大致的用一下,就原则性错误了?

【在 z****e 的大作中提到】
: 我没说iterator 绝对没有pointer arithmetic啊,要就自己overload。但是比如STL,
: 除了random access类型的,其他运算符都不全,所以我说"usually"也没说错啊。
: iterator 不要求管理资源不代表不能管理资源啊。所以我也没说错啊。iterator本身
: 就是一种design pattern啊,你想让它作甚,就作甚啊。
: iterator是smart pointer,我也没说错啊。关于什么是精确的smart pointer,不是现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?
: 如果说iterator是naked pointer,没错,但是这个题目就没什么可答的了。
: 我还是觉得,我可能语言没有整理好,就是条理性差些,侧重点答的不好,
: 但是没有原则性错误。
: 以上你逐条批判的话,我也都防住了。

z****e
发帖数: 2024
13
我错了!
我认识到自己的错误了!
我要真心悔过!

【在 X****r 的大作中提到】
: 除非所有的iterator都是smart pointer,你说iterator是smart pointer
: 就是错误的。某些iterator是smart pointer不改变这一点。
:
: 现在还
: 有标准的争论么? 我这里大致的用一下,就原则性错误了?

t****t
发帖数: 6806
14
这个是哪本书说的啊。要说iterator是generalized pointer就对了。反正你面试要这么
说肯定没分。
通常现在大家接受的说法是,smart pointer不需要手工delete的pointer. iterator基
本上没这功能。C++里shared_ptr算是一个。auto_ptr勉强沾边。

【在 z****e 的大作中提到】
: 我说过了,iterator 是 smart pointer 这个不是我先发明的,是书上说的,好几本都
: 出现过这种说法。
: 这是一个说法,至于什么才是 “smart pointer ”,就是文字游戏和标注之争了。
: 我完全同意iterator在STL里边不能resource management.但是不带表不能设计一种具有此工能iterator啊。
: 我思过去了,不敢和二位恩师争。
: 我错了!
:
: patt

z****e
发帖数: 2024
15
Book Exploring C++
Publisher Apress
DOI 10.1007/978-1-4302-0352-0
Copyright 2009
ISBN 978-1-59059-749-1 (Print) 978-1-4302-0352-0 (Online)
Part Part 4
DOI 10.1007/978-1-4302-0352-0_60
Pages 567-580
Subject Collection Professional and Applied Computing
SpringerLink Date Saturday, February 07, 2009
我就google随便一搜,
其中一句:
An iterator can ensure that it does not advance past the end of a container.
Thus, iterators are smart pointers because they can be really, really smart

【在 t****t 的大作中提到】
: 这个是哪本书说的啊。要说iterator是generalized pointer就对了。反正你面试要这么
: 说肯定没分。
: 通常现在大家接受的说法是,smart pointer不需要手工delete的pointer. iterator基
: 本上没这功能。C++里shared_ptr算是一个。auto_ptr勉强沾边。

z****e
发帖数: 2024
16
我不但没分,还和面试官强行狡辩。

这么

【在 t****t 的大作中提到】
: 这个是哪本书说的啊。要说iterator是generalized pointer就对了。反正你面试要这么
: 说肯定没分。
: 通常现在大家接受的说法是,smart pointer不需要手工delete的pointer. iterator基
: 本上没这功能。C++里shared_ptr算是一个。auto_ptr勉强沾边。

d****p
发帖数: 685
17
iterator for certain containers (eg vector> is implemented as raw pointer -
you can think as:
template
class Iterator
{
....T* _M_current;
}
After optimization is turned on, the thin wrapper will be thown away by
compiler and you will have the
same raw metal code.
For example, you have two functions:
int foo1(std::vector::const_iterator intItr)
{ return *intItr + 1; }
and
int foo2(const int* intPtr)
{ return *intPtr + 1; }
Both functions generate the around same assembly snipet:

【在 z****e 的大作中提到】
: iterator is a smart pointer.
: all thing related is about smart pointer vs naked pointer.
: such like:
: 1.resource management
: 2.exception safety
: 3.reference counter
: etc,
: smart pointer has cp-ctor and op= overloaded.
: but using smart pointer, usually you lose the usual pointer arithmetic.
: to calculate smart pointer difference, you need to call distance(first, last).

t****t
发帖数: 6806
18
广义上讲,说smart pointer就是pointer with extra features也不能算错。但是这个
说法并不是被广泛接受的。如果你要用,一定要说明你说的smart pointer是什么意思。
在C++里,如果不加说明,smart pointer就是指我刚才说的意思,也就是不需要手工de
lete。

【在 z****e 的大作中提到】
: Book Exploring C++
: Publisher Apress
: DOI 10.1007/978-1-4302-0352-0
: Copyright 2009
: ISBN 978-1-59059-749-1 (Print) 978-1-4302-0352-0 (Online)
: Part Part 4
: DOI 10.1007/978-1-4302-0352-0_60
: Pages 567-580
: Subject Collection Professional and Applied Computing
: SpringerLink Date Saturday, February 07, 2009

z****e
发帖数: 2024
19
你怎么换头像了?
头像还不清不楚的。

-

【在 d****p 的大作中提到】
: iterator for certain containers (eg vector> is implemented as raw pointer -
: you can think as:
: template
: class Iterator
: {
: ....T* _M_current;
: }
: After optimization is turned on, the thin wrapper will be thown away by
: compiler and you will have the
: same raw metal code.

d****p
发帖数: 685
20
Nope. it is my 1st pic.
I deliberately keep it blur - it is actually a line of C code

【在 z****e 的大作中提到】
: 你怎么换头像了?
: 头像还不清不楚的。
:
: -

相关主题
弱问c++ iterator 和 pointer区别请问delete的问题
树的前序遍历大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.
一个C#使用C++.NET类库的问题[新手]一直都把C++当Java写,有啥严重不妥?
进入Programming版参与讨论
z****e
发帖数: 2024
21
还有能不能reference count,也是何谓smart pointer标准争论焦点之一啊。
所以你说的auto_ptr勉强沾边,也是这个意思吧。

思。
de

【在 t****t 的大作中提到】
: 广义上讲,说smart pointer就是pointer with extra features也不能算错。但是这个
: 说法并不是被广泛接受的。如果你要用,一定要说明你说的smart pointer是什么意思。
: 在C++里,如果不加说明,smart pointer就是指我刚才说的意思,也就是不需要手工de
: lete。

z****e
发帖数: 2024
22
why blur?
how does the code look like?

【在 d****p 的大作中提到】
: Nope. it is my 1st pic.
: I deliberately keep it blur - it is actually a line of C code

d****p
发帖数: 685
23

patt
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A very good book. I read it twice a year :-)

【在 t****t 的大作中提到】
: 等等,你这答案根本不对。iterator is not smart pointer. at all.
: which iterator is smart pointer (that can manage resource) in STL? none.
: and design pattern is not "想让它作甚,就作甚". in addition, the design patt
: ern iterator is not what asked here.
: recommended book: effective STL...
:
: 现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?

z****e
发帖数: 2024
24
i read it twice early this year. hehe. but i think my understanding is not
as deep as you. it is really an excellent book worth to read many times.

【在 d****p 的大作中提到】
:
: patt
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A very good book. I read it twice a year :-)

z****e
发帖数: 2024
25
对我的批斗会该结束了吧?
导师们都散了吧!歇歇睡吧。
d****p
发帖数: 685
26
if (interviewee == ZaoXie)
...KeepAskingQuestionsUntilHeCannotAnswer();

【在 z****e 的大作中提到】
: why blur?
: how does the code look like?

h****b
发帖数: 157
27
想不到大家这么踊跃热情 : )
如果问问什么stl不用pointer (我被问到),可不可以回答 iterator的operator被重载
过,比
pointer更容易使用,比如list的iterator++,直接指向下一个link list element。
另外,请问一下:vector是怎么实现的?我知道是dynamic array, continuous memory
,不
过如果需要resize (e.g. increase size), 怎么能保证还是continuous memory?
谢了

【在 X****r 的大作中提到】
: That is not the right answer.
: In C++, Iterator is a concept. A concept is a set of requirement for
: a type. For example, a random access iterator must allow operators
: ++, +, +=, --, -, -=, [], etc. with the right semantics, while a
: forward iterator only needs to have ++. Naked pointers can be
: iterators, because they already have these operators built-in.
: User-defined types can also be iterators, as long as they overload
: required operators correctly.
: So the difference between iterator and pointer

X****r
发帖数: 3557
28
你这么说也可以。如果是我的话会回答为了算法的普适性,STL需要区分一个类型的界面
(以及相应的功能)和其具体实现。iterator就是这样一种抽象。通过使用iterator
这个概念,而不是它的某一种具体形式(比如指针),不同容器的区别对于不关心这些
区别的算法来说就被统一了,从而简化了代码。
vector的问题,重新分配然后把内容移动过去就可以了。你可以直接看STL的源代码啊。

memory

【在 h****b 的大作中提到】
: 想不到大家这么踊跃热情 : )
: 如果问问什么stl不用pointer (我被问到),可不可以回答 iterator的operator被重载
: 过,比
: pointer更容易使用,比如list的iterator++,直接指向下一个link list element。
: 另外,请问一下:vector是怎么实现的?我知道是dynamic array, continuous memory
: ,不
: 过如果需要resize (e.g. increase size), 怎么能保证还是continuous memory?
: 谢了

S**I
发帖数: 15689
29
我的理解是:iterator是一种design pattern,pointer不是。

界面
啊。

【在 X****r 的大作中提到】
: 你这么说也可以。如果是我的话会回答为了算法的普适性,STL需要区分一个类型的界面
: (以及相应的功能)和其具体实现。iterator就是这样一种抽象。通过使用iterator
: 这个概念,而不是它的某一种具体形式(比如指针),不同容器的区别对于不关心这些
: 区别的算法来说就被统一了,从而简化了代码。
: vector的问题,重新分配然后把内容移动过去就可以了。你可以直接看STL的源代码啊。
:
: memory

l******e
发帖数: 12192
30
tell him:
iterators are evil!
-Andrei Alexandrescu
haha

【在 h****b 的大作中提到】
: 是不是map:iterator可以指向下一个元素,所以,用iterator自动traverse tree,因为
: map是
: sorted.
: 还有什么区别?

相关主题
C++的smart pointer注定是个二流的东西stl的interator是一种smart pointer吗
琢磨了一下c++ smart pointer,发现不能到处用C++ 菜鸟问一个关于template 的问题。
c++ pointers are iterators, why?deque的pointer和reference是怎么回事?
进入Programming版参与讨论
h****b
发帖数: 157
31
呵呵,了解。3x

界面
啊。

【在 X****r 的大作中提到】
: 你这么说也可以。如果是我的话会回答为了算法的普适性,STL需要区分一个类型的界面
: (以及相应的功能)和其具体实现。iterator就是这样一种抽象。通过使用iterator
: 这个概念,而不是它的某一种具体形式(比如指针),不同容器的区别对于不关心这些
: 区别的算法来说就被统一了,从而简化了代码。
: vector的问题,重新分配然后把内容移动过去就可以了。你可以直接看STL的源代码啊。
:
: memory

k*******d
发帖数: 1340
32
我印象中iterator对resource allocation没怎么支持吧,它支持RAII?貌似不是,所以
,把他和tr1::shared_ptr等同是不够准确的,当然,如果说广义的smart pointer那的
确有联系
1 (共1页)
进入Programming版参与讨论
相关主题
大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.关于不同类型的smart pointer
[新手]一直都把C++当Java写,有啥严重不妥?请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)
C++的smart pointer注定是个二流的东西is smart_ptr really that good?
琢磨了一下c++ smart pointer,发现不能到处用Use std::swap to exchange pointers and iterators
c++ pointers are iterators, why?Pointer to iterator?
stl的interator是一种smart pointer吗请教 C++ std::list iterator 对 template class pointer 的应用问题
C++ 菜鸟问一个关于template 的问题。c++ 里用到pointer 的地方我们尽可能用smart pointer吗?
deque的pointer和reference是怎么回事?弱问c++ iterator 和 pointer区别
相关话题的讨论汇总
话题: iterator话题: pointer话题: smart话题: c++话题: stl