473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
11 3043

"Vivi Orunitia" <Vi**@blackmage village.com> wrote in message
news:Xn******** *************** ***********@199 .45.49.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::poin ter.
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::poin ter and container::iter ator
are actually same.

Best wishes,
Sharad
Jul 22 '05 #2
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::poin ter.
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::poin ter and
container::iter ator 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
On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:
Iterator could be some other class or container::poin ter.
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::poin ter and
container::iter ator 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::itera tor 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

"Vivi Orunitia" <Vi**@blackmage village.com> wrote in message
news:Xn******** *************** ***********@199 .45.49.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::poin ter.
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::poin ter and
container::iter ator 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<iostre am>
using namespace std;

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

IntList::iterat or itr = li.begin();
// IntList::pointe r p = li.begin(); //ERROR

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

}
Jul 22 '05 #5

"Vivi Orunitia" <Vi**@blackmage village.com> wrote in message
news:Xn******** *************** ***********@199 .45.49.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

"Vivi Orunitia" <Vi**@blackmage village.com> wrote in message
news:Xn******** *************** ***********@199 .45.49.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::poin ter.
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::poin ter and
container::iter ator 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

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
On Tue, 03 Feb 2004 08:46:58 GMT, Vivi Orunitia
<Vi**@blackmage village.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
generalization s 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::poin ter.
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::poin ter and
container::iter ator 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::iterato r. 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
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.l earn.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2506
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help would be muchly appreciated. Apologies for the long post - I couldn't find a shorter way to
16
2708
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff. 'this' pointer from items is a freeby :>. the problem is, std...
5
2767
by: edward.birch | last post by:
Can anyone see anything wrong with the following code? (CONTAINER can be list, vector, set, ...) template <class T> void Destroy(T * p) { delete p; } void CleanUp(std::CONTAINER<ContainerType *> & Container) { std::foreach(Container.begin(), Container.end(), Destroy<ContainerType>);
20
5117
by: Manuel | last post by:
Hi. Before all, please excuse me for bad english and for very newbie questions. I hope to don't boring you. I'm trying to write a very simple GUI using openGL. So I'm writing some different widgets classes, like buttons, images, slider, etc... Each class has a draw() method.
32
69692
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
2
1842
by: esuvs | last post by:
Hi, I would like to change the behavior of the std::list so that if I set an iterator to the last element and increment it I would like it to point to the first element, and vice vesa. That is, i would like to use it as a circular buffer. Is this possble? Given that the linked list is probably just a set of nodes and 'next' pointers it doesn't seem unreasonable (just set the last pointer to point at the start) but I suspect the std::list...
12
9463
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
17
3191
by: mosfet | last post by:
Could someone tell me why it's considered as bad practice to inherit from STL container ? And when you want to customize a STL container, do you mean I need to write tons of code just to avoid to derive from it ?
10
6311
by: Chris Forone | last post by:
Hello Group, there is some memberfunc for std::valarray to return a pointer to the first element in the array. How do i use this? Thanx a lot. HAND Chris
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.