Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Vivi Orunitia
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Sharad Kala
Guest
 
Posts: n/a
#2: Jul 22 '05

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



"Vivi Orunitia" <Vivi@blackmagevillage.com> wrote in message
news:Xns94841E297AB88Viviblackmagevillage@199.45.4 9.11...[color=blue]
> 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?[/color]

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


Vivi Orunitia
Guest
 
Posts: n/a
#3: Jul 22 '05

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


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.spam_sharadk_ind@yahoo.com> wrote in
news:bvnlp8$uu0i2$1@ID-221354.news.uni-berlin.de:
[color=blue]
>
>
> 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.
>
>[/color]

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 :)
Martijn Lievaart
Guest
 
Posts: n/a
#4: Jul 22 '05

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


On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:
[color=blue][color=green]
>> 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.
>>
>>[/color]
>
> 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.[/color]

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



Sharad Kala
Guest
 
Posts: n/a
#5: Jul 22 '05

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



"Vivi Orunitia" <Vivi@blackmagevillage.com> wrote in message
news:Xns948426AD92D26Viviblackmagevillage@199.45.4 9.11...[color=blue]
> 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.spam_sharadk_ind@yahoo.com> wrote in
> news:bvnlp8$uu0i2$1@ID-221354.news.uni-berlin.de:
>[color=green]
> >
> >
> > 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.
> >
> >[/color]
>
> 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.[/color]

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

}


John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

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



"Vivi Orunitia" <Vivi@blackmagevillage.com> wrote in message
news:Xns94841E297AB88Viviblackmagevillage@199.45.4 9.11...[color=blue]
> 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[/color]

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


Chris Theis
Guest
 
Posts: n/a
#7: Jul 22 '05

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



"Vivi Orunitia" <Vivi@blackmagevillage.com> wrote in message
news:Xns948426AD92D26Viviblackmagevillage@199.45.4 9.11...[color=blue]
> 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.spam_sharadk_ind@yahoo.com> wrote in
> news:bvnlp8$uu0i2$1@ID-221354.news.uni-berlin.de:
>[color=green]
> >
> >
> > 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.
> >
> >[/color]
>
> 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 :)[/color]

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



Sharad Kala
Guest
 
Posts: n/a
#8: Jul 22 '05

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



[color=blue]
> You probably did your testing with std::vector or std::string.[/color]

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.


tom_usenet
Guest
 
Posts: n/a
#9: Jul 22 '05

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


On Tue, 03 Feb 2004 08:46:58 GMT, Vivi Orunitia
<Vivi@blackmagevillage.com> wrote:
[color=blue]
>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.[/color]

Right, a pointer is a model of a random access iterator. That is, a
pointer is a type of iterator.
[color=blue]
>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.[/color]

Right - non-bidirectional iterators don't provide --.
[color=blue]
>"Sharad Kala" <no.spam_sharadk_ind@yahoo.com> wrote in
>news:bvnlp8$uu0i2$1@ID-221354.news.uni-berlin.de:
>[color=green]
>>
>>
>> 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.
>>
>>[/color]
>
>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.[/color]

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
Thomas Matthews
Guest
 
Posts: n/a
#10: Jul 22 '05

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


Vivi Orunitia wrote:
[color=blue]
> 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[/color]

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

Vivi Orunitia
Guest
 
Posts: n/a
#11: Jul 22 '05

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


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
Martijn Lievaart
Guest
 
Posts: n/a
#12: Jul 22 '05

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


On Tue, 03 Feb 2004 18:43:03 +0000, Vivi Orunitia wrote:
[color=blue]
> 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.[/color]

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

Closed Thread


Similar C / C++ bytes