By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,917 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,917 IT Pros & Developers. It's quick & easy.

iterator for custom class

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Hi Roland,
Just 'export' the std::list iterators and the sort member function:
Thanks!

Regards
Max
Jun 12 '07 #7

P: n/a
Hi Victor,

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

Regards
Max
Jun 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.