By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

std::container::iterator vs std::container::pointer

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Vivi Orunitia" <Vi**@blackmagevillage.com> wrote in message
news:Xn**********************************@199.45.4 9.11...
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?


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
Jul 22 '05 #2

P: n/a
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 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.


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 :)
Jul 22 '05 #3

P: n/a
On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:
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.


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.


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

Jul 22 '05 #4

P: n/a

"Vivi Orunitia" <Vi**@blackmagevillage.com> wrote in message
news:Xn**********************************@199.45.4 9.11...
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 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.


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.


Probably you are using vectors and iterators are pointers on your
implementation.
Try using a list and see even if the code compiles!

#include<list>
#include<iostream>
using namespace std;

int main(){
typedef list<int> IntList;
IntList li;
li.push_back(5);
li.push_back(7);

IntList::iterator itr = li.begin();
// IntList::pointer p = li.begin(); //ERROR

IntList::pointer p = &(*itr);
cout << *(++itr); // prints 7
cout << *(++p); // god knows, lucky if you get a crash here

}
Jul 22 '05 #5

P: n/a

"Vivi Orunitia" <Vi**@blackmagevillage.com> wrote in message
news:Xn**********************************@199.45.4 9.11...
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


You probably did your testing with std::vector or std::string. Pointers and
iterators are likely to be the same for these classes, they are very
unlikely to be the same for other classes.

John
Jul 22 '05 #6

P: n/a

"Vivi Orunitia" <Vi**@blackmagevillage.com> wrote in message
news:Xn**********************************@199.45.4 9.11...
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 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.


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 :)


As Sharad already pointed out iterators might be implemented in terms of
pointers (often found for vector container implementations). However, you
should not rely on this! For example the standard signature for the begin
function of a container is to return an iterator.
You might find implementations where the following line is valid:

vector<char> Data;
// fill vector
printf("%s", Data.begin() );

This might work but it's certainly NOT portable!! Hence, it's always safe to
stick to the iterator definition no matter which terms the implementation is
done in.

Regards
Chris

Jul 22 '05 #7

P: n/a

You probably did your testing with std::vector or std::string.


Yes and additionally std::vector or std::string on his *implementation* used
pointers as iterators.
There are some vector implementations that do not use pointers as iterators.
Additionally code like vec.begin()++ would be invalid in case of pointers.
Jul 22 '05 #8

P: n/a
On Tue, 03 Feb 2004 08:46:58 GMT, Vivi Orunitia
<Vi**@blackmagevillage.com> wrote:
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.
Right, a pointer is a model of a random access iterator. That is, a
pointer is a type of iterator.
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.
Right - non-bidirectional iterators don't provide --.
"Sharad Kala" <no*****************@yahoo.com> wrote in
news:bv************@ID-221354.news.uni-berlin.de:


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.


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.


When ::iterator isn't the same as ::pointer - you'll get compiler
errors. There is only one situation where they might be the same -
using std::vector with certain (mostly old) standard library
implementations. Most modern libraries have a class type for
vector::iterator. You can convert an iterator into a pointer but not
vice versa. e.g.

int* p = &*mylist.begin();

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9

P: n/a
Vivi Orunitia wrote:
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


A good example of the difference is when using a binary tree
container, or a linked list. To point to the next element
in an collinear sequence, one can use a pointer and just
increment the pointer. However, with a linked list or
tree, the elements may not be adjacient to each other,
so a plain increment on a pointer will not work.

Iterators allow one to write a function to point to
the next element in the contain. So for a singly
linked list, the operator++ may be:
{
return next_link;
}
For other data structures, the operation may be
more complex.

Many iterator implementations will try their best to
masquarade as a pointer; which helps out with accessing
elements in a container.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #10

P: n/a
Thanks for all the response guys. I think the distinction is starting to
become clearer for me now :)

I always went with ::iterator for the std containers I use as far as I can
remember but I was looking up the std reference the other day and noticed
they also had ::pointer. So it just got me asking some questions like what
their difference were and if ::iterators existed why do we have ::pointers
and questions like that.

Thanks
Jul 22 '05 #11

P: n/a
On Tue, 03 Feb 2004 18:43:03 +0000, Vivi Orunitia wrote:
Thanks for all the response guys. I think the distinction is starting to
become clearer for me now :)

I always went with ::iterator for the std containers I use as far as I can
remember but I was looking up the std reference the other day and noticed
they also had ::pointer. So it just got me asking some questions like what
their difference were and if ::iterators existed why do we have ::pointers
and questions like that.


The thing I think no one mentioned up to now, is the idea behind
container::pointer. This could conceivably be different from a real
pointer for a container that "proxies" for some underlying architecture.
In that case, the pointer type would be a proxy class that implemented
pointer-like behaviour (just like an iterator, but slightly different).

Unfortunately, this would not be a STL container, it was a nice experiment
that failed in the end. The details escape me at the moment (where /are/
all my Meyers books gone), I'm sure someone will fill this in, but for
anything in the standard library, except vector<bool> you can assume
container<T>::pointer is a T*. But using container<T>::pointer (and
reference!) in your own generic code where appropriate makes your
algorithm well behaved with types that implement such a proxy-pointer-type.

HTH,
M4

Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.