iterator for custom class

M

Max Odendahl

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
 
V

Victor Bazarov

Max said:
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
 
M

Max Odendahl

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
 
V

Victor Bazarov

Max said:
Hi,



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
 
R

Roland Pibinger

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<int> myList;
};
 
J

James Kanze

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.)
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.
 
M

Max Odendahl

Hi Victor,

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

Regards
Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top