Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::container::iterator vs std::container::pointer

Reply
Thread Tools

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

 
 
Vivi Orunitia
Guest
Posts: n/a
 
      02-03-2004
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 :ointer?

I did a quick experiment and replaced all the ::iterator in my code with
:ointer 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
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      02-03-2004

"Vivi Orunitia" <(E-Mail Removed)> wrote in message
news:Xns94841E297AB88Viviblackmagevillage@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 :ointer?
>
> I did a quick experiment and replaced all the ::iterator in my code with
> :ointer 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:ointer.
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:ointer and container::iterator
are actually same.

Best wishes,
Sharad


 
Reply With Quote
 
 
 
 
Vivi Orunitia
Guest
Posts: n/a
 
      02-03-2004
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" <(E-Mail Removed)> wrote in
news:bvnlp8$uu0i2$(E-Mail Removed)-berlin.de:

>
>
> Iterator could be some other class or container:ointer.
> 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:ointer 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 :ointer interchangably would cause potential problems etc.

Thanks for the response
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      02-03-2004
On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:

>> Iterator could be some other class or container:ointer.
>> 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:ointer 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 :ointer 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



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      02-03-2004

"Vivi Orunitia" <(E-Mail Removed)> wrote in message
news:Xns948426AD92D26Viviblackmagevillage@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" <(E-Mail Removed)> wrote in
> news:bvnlp8$uu0i2$(E-Mail Removed)-berlin.de:
>
> >
> >
> > Iterator could be some other class or container:ointer.
> > 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:ointer 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 :ointer 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:ointer p = li.begin(); //ERROR

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

}


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-03-2004

"Vivi Orunitia" <(E-Mail Removed)> wrote in message
news:Xns94841E297AB88Viviblackmagevillage@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 :ointer?
>
> I did a quick experiment and replaced all the ::iterator in my code with
> :ointer 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


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      02-03-2004

"Vivi Orunitia" <(E-Mail Removed)> wrote in message
news:Xns948426AD92D26Viviblackmagevillage@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" <(E-Mail Removed)> wrote in
> news:bvnlp8$uu0i2$(E-Mail Removed)-berlin.de:
>
> >
> >
> > Iterator could be some other class or container:ointer.
> > 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:ointer 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 :ointer 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



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      02-03-2004


> 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.


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      02-03-2004
On Tue, 03 Feb 2004 08:46:58 GMT, Vivi Orunitia
<(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in
>news:bvnlp8$uu0i2$(E-Mail Removed)-berlin.de:
>
>>
>>
>> Iterator could be some other class or container:ointer.
>> 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:ointer 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 :ointer interchangably would cause potential problems etc.


When ::iterator isn't the same as :ointer - 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
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      02-03-2004
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 :ointer?
>
> I did a quick experiment and replaced all the ::iterator in my code with
> :ointer 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments