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

iterator for custom class

Hi,

my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class the best
solution for this and how to do this?
I would like to sort the list according to different criteria before, so I
would like to offer different iterators, which then sort first based on the
type of iterator constructed.

Any code sample or link would be nice.

Best regards
Max
Jun 11 '07 #1
7 3807
Max Odendahl wrote:
my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class
the best solution for this and how to do this?
No. You can expose the entire list by returning it by reference from
some kind of accessor function. So, it all depends on how you define
"best".
I would like to sort the list according to different criteria before,
so I would like to offer different iterators, which then sort first
based on the type of iterator constructed.
Iterators don't sort, they aren't supposed to. Iterators iterate.
If you need to sort the list, sort it. But what if I need to access
your list sorted in two different ways at the same time? How would
a custom iterator help? It wouldn't, by itself. It seems that you
need to keep a bunch of custom lists of iterators into your "master"
list, and iterate those using your custom iterators. Sounds like
a bit of a mess, but that's the most generic solution I could think
of. Whether it's the "best" depends on the definition of 'betst.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 11 '07 #2
Hi,

Iterators don't sort, they aren't supposed to. Iterators iterate.
If you need to sort the list, sort it.
I'm aware about that, the idea was the following:

In the constructor of my iterator, the sort method of my list is called with
the sort function depending on which iterator is constructed.
Then the iterator can be used to get the elements from the outside.. In the
destructor of my iterator, the list is then resorted to the regular sort
order

But I do not know how to make an iterator for my class to loop over the list
elements inside.

Regards
Max
Jun 11 '07 #3
Max Odendahl wrote:
Hi,

>Iterators don't sort, they aren't supposed to. Iterators iterate.
If you need to sort the list, sort it.

I'm aware about that, the idea was the following:

In the constructor of my iterator, the sort method of my list is
called with the sort function depending on which iterator is
constructed. Then the iterator can be used to get the elements from the
outside..
In the destructor of my iterator, the list is then resorted to the
regular sort order

But I do not know how to make an iterator for my class to loop over
the list elements inside.
The problem is deeper than making the iterator to loop. In order to
loop you just need to make it store another iterator and then in the
operator ++ (or --) you change the stored iterator and in the op *
and op -you use the stored iterator (by calling its respective ops).

But what if I want to make a copy of your iterator? Is that OK? What
if I want to iterate using two different custom iterators at the same
time? The constructors sort, so after constructing one iterator, as
soon as I construct a slightly different custom iterator, the list is
screwed for the first one, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 11 '07 #4
On Tue, 12 Jun 2007 03:22:32 +1000, "Max Odendahl" wrote:
>my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class the best
solution for this and how to do this?
I would like to sort the list according to different criteria before, so I
would like to offer different iterators, which then sort first based on the
type of iterator constructed.

Any code sample or link would be nice.
Just 'export' the std::list iterators and the sort member function:

#include <list>

class MyClass {
public:
typedef std::list<int>::iterator iterator;

iterator begin() { return myList.begin(); }
iterator end() { return myList.end(); }

template<class Pred>
void sort(Pred pred) { myList.sort(pred); }

void sort() { myList.sort(); }

private:
std::list<intmyList;
};
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 11 '07 #5
On Jun 11, 7:31 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Max Odendahl wrote:
my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class
the best solution for this and how to do this?
No. You can expose the entire list by returning it by reference from
some kind of accessor function. So, it all depends on how you define
"best".
Generally, I've found this not to be a very good idea. You
decide the minimum that you have to guarantee, and wrap the
iterator of whatever container you're using to only support
that. (For example, you might decide only to "expose" a forward
iterator, in order to keep your options open, even though the
current implementation uses std::vector.)
I would like to sort the list according to different criteria before,
so I would like to offer different iterators, which then sort first
based on the type of iterator constructed.
Iterators don't sort, they aren't supposed to. Iterators iterate.
Over a sequence. And a sequence has order. Iterators thus
expose order, and at least conceptually, could induce it.

In practice, of course, I'm not to sure how you'd go about
providing an iterator which exposes an order not supported by
the underlying container. (It depends, of course. I have
"iterators" over a directory which present the directory in
alphabetical order, regardless of the order on the disk. But in
this case, the iterators actually point into an in memory copy,
in a vector, which has been sorted.) There is nothing wrong,
however, with, say, maintaining all of the elements in a vector,
and then several different sets of pointers to the element, each
sorted according to a different criteron.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 11 '07 #6
Hi Roland,
Just 'export' the std::list iterators and the sort member function:
Thanks!

Regards
Max
Jun 12 '07 #7
Hi Victor,

thanks for you thoughts on this issue, I'll take them into account

Regards
Max
Jun 12 '07 #8

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

Similar topics

1
by: James Aguilar | last post by:
Hey all, I ran into an interesting little problem with defining an iterator in a custom array class the other day. It goes something like this: template <typename T> class Array { public:...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
7
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
8
by: Mateusz Åoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
1
by: Siegfried Heintze | last post by:
What is the minimum I must type to create a custom iterator that will allow me display my iterator on std::cout using std::copy? Thanks, Siegfried
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...
3
by: vasili | last post by:
hello All, I have a simple issue. I defined a custom container, that encloses a std::list, which in turn holds objects that are a simple abstraction of a six position array. Now, i would like...
16
by: arnaudk | last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class...
5
by: maverik | last post by:
Hi all. I'm implementing class AbstractCollection - just a wrap on std::vector - to hide from clients the fact of using std::vector in the base (because in the future I may change vector to a...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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...

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.