![]() |
Web Development »
Trace and Logs »
General
Intermediate
CList IteratorBy Craig HendersonA simple iteration class for MFC's CList linked list |
VC6Win2K, Visual Studio, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
MFC's CList provides a template linked list implementation and works perfectly adequately.
However, iterating through the list can be a little combersome. Code like that below is common place for
processing list elements, but the POSITION variable is meaningless and confusing as it
always points to the next node rather than the current one.
POSITION pos; CList<int, int> list; // add nodes to the list pos = list.GetHeadPosition(); while (pos) { int nElement; nElement = list.GetNext(pos); // do something with nElement }
The iterator class I present here simplifies the use slightly, making the code easier to read
and maintain. It originates from a larger project I wrote where I have a list of objects of type
A, each with their own list of objects of type B, and I needed to
iterate all objects of type B in the system. This class simplified the task immensly.
So using this class, the above code becomes
CList<int, int> list; // linked list TLListIter<int, int> iter(list); // linked list iterator // add nodes to the list iter.begin(); while (iter) { int nElement; nElement = iter++; // do something with nElement }
TLListIter(TLList<TYPE, ARG_TYPE> &__list);
TLListIter(TLListIter<TYPE, ARG_TYPE> &__iter);
Copy constructor to create a new iterator for the same linked list with the same node position.
virtual ~TLListIter();
Virtual class destructor.
operator bool ();
operator ARG_TYPE ();
Linked List argument operator will returns the element at the current node.
TLListIter<TYPE, ARG_TYPE> operator++(int);
TLListIter<TYPE, ARG_TYPE> operator--(int);
Post decrement operator returns the current node and moves the internal position to the previous node.
// these functions will return false if there are no elements in the list
bool begin(void);
Moves to the first node in the list. This function returns false if the linked list is empty.
bool end(void);
Moves to the last node in the list. This function returns false if the linked list is empty.
POSITION GetPosition(void) const;
Returns the internal POSITION variable.
bool. This is because of the
operator bool() used to determine the end of the list.while (nNumber == 7 && iter). Instead, this must be coded while (nNumber
== 7 && iter == true) and then all is fine. Wierd.| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Oct 2000 Editor: Chris Maunder |
Copyright 2000 by Craig Henderson Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |