Like comparators and collections, iterators are defined as class templates. You use templates to instantiate an iterator for a collection containing a particular kind of element. You can create iterators by invoking constructors for a given iterator class, however it's generally easier to use the protocol provided by collections themselves. For example TArrayOf provides two member functions for creating iterators.
template <class AType> class TArrayOf : public TIndexedSequenceOf<AType>, public TPrimitiveArray { public: ... virtual TIteratorOver<AType>* CreateIterator() const; virtual TSequenceOfIterator<AType>* CreateSequenceIterator() const; ... }
TArrayOfIterator<TCollectibleLong> iterator(collection);
Which type of iterator you create depends on the type of protocol you require for iteration. If you only need First, Next, and Remove, use CreateIterator to instantiate an iterator for your collection. This technique is applicable to all collection classes. If, in addition to the basic protocol defined by TIteratorOver, you need Previous, and Last, use CreateSequenceIterator.
A point of confusion arises from the fact that for arrays, CreateSequenceIterator returns a TSequenceOfIterator when it might seem it should return a TArrayOfIterator. Actually, a pointer is returned and even though the pointer is of type TSequenceOfIterator, the object it points to is of type TArrayOfIterator. Because the protocol for iteration is defined by virtual functions, the appropriate behavior for iteration is effected through dynamic binding with the iterators returned by either CreateSequenceIterator or CreateIterator.
As the purpose here is to discuss general protocol for collections, focus on iteration using TIteratorOver objects returned by CreateIterator. Because iterator classes are templatized, the specific type returned by CreateIterator depends on the type of element TArrayOf was declared to contain. For an array declared to hold TCollectibleLongs
TArrayOf<TCollectibleLong>* collection = new TArrayOf<TCollectibleLong>(comparator, NIL);
TIteratorOver<TCollectibleLong>* iterator = collection->CreateIterator();
iterator->First()
TCollectibleLong* number = iterator->First();
number = iterator->Next()
for (TCollectibleLong* number = iterator->First(); number != NIL; number = iterator->Next(), i++) { ... }
void printCollection(TCollectionOf<TCollectibleLong>* collection) { ... }
void printCollection(TCollectionOf<TCollectibleLong>* collection) { int i = 0; // tracks order of elements TIteratorOver<TCollectibleLong>* iterator = collection->CreateIterator(); for (TCollectibleLong* number = iterator->First(); number != NIL; number = iterator->Next(), i++) { long aLong = number->GetValue(); printf("\n%3d> %d", i, aLong); } printf("\n"); delete iterator; }
The following program uses printCollection to print the contents of a collection after for TCollectibleLongs are added to it.
Sample program
using printCollection
The output from this program shows the index value of the element followed by the value of the TCollectibleLong at the index position within the collection. // Copyright (c) 1994 Taligent, Inc. All Rights Reserved.
#ifndef Taligent_COLLECTIONS
#include <Collections.h>
#endif
#ifndef _H_STDIO
#include <stdio.h>
#endif
void printCollection(TCollectionOf<TCollectibleLong>* collection)
{
int i = 0; // tracks order of elements
TIteratorOver<TCollectibleLong>* iterator = collection->CreateIterator();
for (TCollectibleLong* number = iterator->First();
number != NIL;
number = iterator->Next(), i++) {
long aLong = number->GetValue();
printf("\n%3d> %d", i, aLong);
}
printf("\n");
delete iterator;
}
int main()
{
TArrayOf<TCollectibleLong>* collection =
new TArrayOf<TCollectibleLong>(NIL, NIL);
collection->Add(new TCollectibleLong(0));
collection->Add(new TCollectibleLong(1));
collection->Add(new TCollectibleLong(2));
collection->Add(new TCollectibleLong(3));
printCollection(collection);
collection->DeleteAll();
delete collection;
return 0;
}
0> 0
1> 1
2> 2
3> 3
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.