473,396 Members | 1,765 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,396 software developers and data experts.

Iterators and containers STL

Hi,

I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far has led me to believe to the following

A) Client has to declare the container, populate it and THEN
instatiate the iterator by passing the container as an argument. This
approach does not require deleting the iterator object as it is
declared on the stack (local). However, the client must know the type
of the container class (whether it is a List, etc)

B)polymorphic iterators use the factory method to create the iterator
when the client requests it. This apporach will require the client to
delete the iterator at some stage.

The second approach suggests that the container provides only 1 method
to create an iterator... What happens if we like to have a forward
iterator and a backward iterator for a container that uses the B
approach above... does this mean we will have 2 methods in its
interface, eg CreateForwardIterator() and CreateBackwardIterator()?

The problem with deletion can be resolved with a proxy pattern but can
this be inefficient in any way?

The GOF book suggests an interface for the iterator which is textual.
Should I use that interface or adopt the style where one uses pointer
operators?

Now lets say the above questions are answered and I have decided on
which iterator (A or B) to use and established its interface. What
will my container class inherit from? Will it inherit from an
appropriate STL container class? If so doesnt this mean that I dont
need to implement any iterator classes as STL already supports
iterators for its containers. If I did inherit from a container class
in STL would anyone see the benefits of wrapping up the STL iterators
in my own classes.

Obviously I could just write my container classes from scratch but
that wouldnt be taking advantage of code reuse. Can I implement the
iterator pattern and use STL at the same time...

I hope I have made sense....

Many Thanks

Merlin
Jul 22 '05 #1
4 3516
"Merlin" <me********@hotmail.com> wrote
Hi,

I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far has led me to believe to the following

A) Client has to declare the container, populate it and THEN
instatiate the iterator by passing the container as an argument. This
approach does not require deleting the iterator object as it is
declared on the stack (local). However, the client must know the type
of the container class (whether it is a List, etc)

B)polymorphic iterators use the factory method to create the iterator
when the client requests it. This apporach will require the client to
delete the iterator at some stage.

The second approach suggests that the container provides only 1 method
to create an iterator... What happens if we like to have a forward
iterator and a backward iterator for a container that uses the B
approach above... does this mean we will have 2 methods in its
interface, eg CreateForwardIterator() and CreateBackwardIterator()?

The problem with deletion can be resolved with a proxy pattern but can
this be inefficient in any way?

The GOF book suggests an interface for the iterator which is textual.
Should I use that interface or adopt the style where one uses pointer
operators?

Now lets say the above questions are answered and I have decided on
which iterator (A or B) to use and established its interface. What
will my container class inherit from? Will it inherit from an
appropriate STL container class? If so doesnt this mean that I dont
need to implement any iterator classes as STL already supports
iterators for its containers. If I did inherit from a container class
in STL would anyone see the benefits of wrapping up the STL iterators
in my own classes.

Obviously I could just write my container classes from scratch but
that wouldnt be taking advantage of code reuse. Can I implement the
iterator pattern and use STL at the same time...


Why don't you just look at how the standard containers implement their iterator
mechanism and emulate that. Better yet, why not just use standard containers?

Claudio Puviani
Jul 22 '05 #2

"Merlin" <me********@hotmail.com> wrote in message
news:12**************************@posting.google.c om...
Hi,

I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far has led me to believe to the following

A) Client has to declare the container, populate it and THEN
instatiate the iterator by passing the container as an argument. This
approach does not require deleting the iterator object as it is
declared on the stack (local). However, the client must know the type
of the container class (whether it is a List, etc)
There are implementations of the iterator pattern which follow this way. But
(IMHO) the better approach is the one favored by the standard library
(former STL) with the container supplying an appropriate iterator. This is
much more elegant and less cumbersome because otherwise one would have to
handle cases where for example a user tries to pass a container to a random
iterator but the container does not supply random element access.

B)polymorphic iterators use the factory method to create the iterator
when the client requests it. This apporach will require the client to
delete the iterator at some stage.

[SNIP]
Obviously I could just write my container classes from scratch but
that wouldnt be taking advantage of code reuse. Can I implement the
iterator pattern and use STL at the same time...


Of course you can implement the iterator pattern and use the STL but the
question is rather why would you go all the way and redo work somebody has
already done (& optimized!!) for you. Why not go ahead and use the standard
containers and their iterators?

Regards
Chris
Jul 22 '05 #3
"Claudio Puviani" <pu*****@hotmail.com> wrote in message news:<Ii*********************@news4.srv.hcvlny.cv. net>...
"Merlin" <me********@hotmail.com> wrote
Hi,

I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far has led me to believe to the following

A) Client has to declare the container, populate it and THEN
instatiate the iterator by passing the container as an argument. This
approach does not require deleting the iterator object as it is
declared on the stack (local). However, the client must know the type
of the container class (whether it is a List, etc)

B)polymorphic iterators use the factory method to create the iterator
when the client requests it. This apporach will require the client to
delete the iterator at some stage.

The second approach suggests that the container provides only 1 method
to create an iterator... What happens if we like to have a forward
iterator and a backward iterator for a container that uses the B
approach above... does this mean we will have 2 methods in its
interface, eg CreateForwardIterator() and CreateBackwardIterator()?

The problem with deletion can be resolved with a proxy pattern but can
this be inefficient in any way?

The GOF book suggests an interface for the iterator which is textual.
Should I use that interface or adopt the style where one uses pointer
operators?

Now lets say the above questions are answered and I have decided on
which iterator (A or B) to use and established its interface. What
will my container class inherit from? Will it inherit from an
appropriate STL container class? If so doesnt this mean that I dont
need to implement any iterator classes as STL already supports
iterators for its containers. If I did inherit from a container class
in STL would anyone see the benefits of wrapping up the STL iterators
in my own classes.

Obviously I could just write my container classes from scratch but
that wouldnt be taking advantage of code reuse. Can I implement the
iterator pattern and use STL at the same time...


Why don't you just look at how the standard containers implement their iterator
mechanism and emulate that. Better yet, why not just use standard containers?

Claudio Puviani

Hi Claudio

I have looked at them and I like what I see. I just want to get the
container behaviour in classes of my own through inheritance. To these
classes I will add additional behaviours. I just wanted to what place
GOF iterator pattern had in what I was trying to do...

Merlin
Jul 22 '05 #4
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
"Merlin" <me********@hotmail.com> wrote in message
news:12**************************@posting.google.c om...
Hi,

I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far has led me to believe to the following

A) Client has to declare the container, populate it and THEN
instatiate the iterator by passing the container as an argument. This
approach does not require deleting the iterator object as it is
declared on the stack (local). However, the client must know the type
of the container class (whether it is a List, etc)


There are implementations of the iterator pattern which follow this way. But
(IMHO) the better approach is the one favored by the standard library
(former STL) with the container supplying an appropriate iterator. This is
much more elegant and less cumbersome because otherwise one would have to
handle cases where for example a user tries to pass a container to a random
iterator but the container does not supply random element access.

B)polymorphic iterators use the factory method to create the iterator
when the client requests it. This apporach will require the client to
delete the iterator at some stage.


[SNIP]
Obviously I could just write my container classes from scratch but
that wouldnt be taking advantage of code reuse. Can I implement the
iterator pattern and use STL at the same time...


Of course you can implement the iterator pattern and use the STL but the
question is rather why would you go all the way and redo work somebody has
already done (& optimized!!) for you. Why not go ahead and use the standard
containers and their iterators?

Regards
Chris

Hi Chris

Say you wanted to create a class for Points(geometric). In there you
would have a Function called

double GetDistance() const;

by inheriting Points from say std::vector you could add this member
function to this class and other methods deemed appropriate. At the
same time Points will be a vector and support all the algorithms that
a vector supports.

Merlin
Jul 22 '05 #5

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

Similar topics

2
by: Ney André de Mello Zunino | last post by:
Hello. A non-modifying algorithm I implemented uses two associative containers from STL: set and map. The elements on those containers are supposed to refer to actual elements which lie on...
2
by: Alexander Stippler | last post by:
Hi, I've got a question concerning iterators. I'm writing some container class and iterators upon it. I have to have typedef typex pointer; typedef typey reference; to be standard conforming....
6
by: Fraser Ross | last post by:
Algorithms cannot be used with input stream iterators? Is copying the range to a temporary container before using the algorithm the usual thing to do? Fraser.
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
4
by: kalita | last post by:
Hi All, typedef std::list<int> Cont; void f(Cont &c1, Cont &c2) { Cont::iterator it = c1.begin(); c1.swap(c2); it == c2.begin(); // is this ill formed? }
19
by: fungus | last post by:
I mentioned earlier to day that I was moving some code from VC++6 to VC++2005 and having trouble with the new iterators. There's all sorts of problems cropping up in the code thanks to this...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
3
by: zr | last post by:
Hi, Does usage of checked iterators and checked containers make code more secure? If so, can that code considered to be reasonably secure?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.