473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

iterator help

Hi all,
I am writing my own container and need an iterator. I am hesitating
if I should my iterator should inherited from std::iterator or just
write my own one. Please give me an idea.

BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent class.

Thanks.
Jul 19 '05 #1
2 2829
"Rex_chaos" <re*******@21cn.com> wrote in message
news:f7************************@posting.google.com ...

I am writing my own container and need an iterator. I am
hesitating if I should my iterator should inherited from
std::iterator or just write my own one. Please give me an
idea.
Generally, you write an iterator that is most appropriate for
accessing the data in your container.
BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent
class.


Most STL implementations define the iterator types as
nested classes. I prefer this technique myself, but I don't
believe there is any harm in defining it outside the container
class.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #2
re*******@21cn.com (Rex_chaos) wrote:
I am writing my own container and need an iterator. I am hesitating
if I should my iterator should inherited from std::iterator or just
write my own one. Please give me an idea.
'std::iterator<>' just typedefs a bunch of types. That's it. If you
don't derive from 'std::iterator<>' you should be sure to [partially]
specialize 'std::iterator_traits<>' for your iterator type because
otherwise the algorithms won't work OK. Due to lack of partial
specialization support with some compilers, it is probably a good idea
to derive from 'std::iterator<>' anyway: originally, this was mostly a
work around.
BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent class.


This one is a little bit tricky. First of all, it does not matter to
user. Since nested classes have no additional rights external class
have, nesting the iterator class only serves a documentation purpose.
With some specialization I found that it is sufficient to specialize
the container while leaving the iterator alone. Also, the semantics of
making an external class a friend are well defined in the standard
which does not apply to nested classes. Thus, I tend to make iterators
external classes. There is, however, no strong argument I'm aware of
which prefers one of the approaches.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #3

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
13
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
5
by: Bill Oliver | last post by:
Help! I am writing an image processing package. For one constructor, I allow creating an image from a map of points and color values. The points are of a "position" class and the colors are...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
5
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.