473,908 Members | 7,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3845
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(pre d); }

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

private:
std::list<intmy List;
};
--
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...@com Acast.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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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
2049
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: typedef T * iterator;
0
1952
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 layer |
7
5194
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: I need to define special iterator values. In particular, I want a null iterator. This is different from end() which means the end of this list. I want an iterator value that is known to just not correspond to any element. It must be possible to...
8
2452
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
3117
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
5736
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 classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
3
4522
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 to serialize the whole newly-defined container, in order to copy the contents to another array. So i thought to define an iterator which represented a "pointer" to the container's data. But, when i feed these iterators to std::copy the compiler...
16
5370
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 can change independently of the interface. using namespace std; class MyContainer { public: // public stuff protected: struct Item {
5
3137
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 list or another DS). So I try to do it in this way: template<typename T> class AbstractCollection { public:
0
11337
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11042
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10536
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9721
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6134
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4770
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3355
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.