Member Functions | |
container() key() operator()() operator++() reset() value() |
#include<rw/tphdict.h> RWTPtrHashMap<K,T,H,EQ> m; RWTPtrHashMap<K,T,H,EQ> itr(m);
If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashDictionaryIterator described in Appendix A.
RWTPtrHashMapIterator is supplied with Tools.h++ 7.x to provide an iterator interface to the Standard Library based collections that has backward compatibility with the container iterators provided in Tools.h++ 6.x.
Iteration over an RWTPtrHashMap is pseudorandom and dependent on the capacity of the underlying hash table and the hash function being used.
The current item referenced by this iterator is undefined after construction or after a call to reset(). The iterator becomes valid after being advanced with either a preincrement or operator().
For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Once this state is reached, continued increments will return a value equivalent to false until reset() is called.
The value type must have operator== defined. This requirement is imposed by the Standard C++ Library.
None
#include<rw/tphdict.h> #include<iostream.h> #include<rw/cstring.h> struct silly_h{ unsigned long operator()(RWCString x) const { return x.length() * (long)x(0); } }; int main(){ RWTPtrHashMap <RWCString,int,silly_h,equal_to<RWCString> > age; RWTPtrHashMapIterator <RWCString,int,silly_h,equal_to<RWCString> > itr(age); age.insert(new RWCString("John"),new int(30)); age.insert(new RWCString("Steve"),new int(17)); age.insert(new RWCString("Mark"),new int(24)); //Duplicate insertion is rejected age.insert(new RWCString("Steve"),new int(24)); for(;++itr;) cout << *itr.key() << "\'s age is " << *itr.value() << endl; return 0; } Program Output (not necessarily in this order) John's age is 30 Mark's age is 24 Steve's age is 17
RWTPtrHashMapIterator<K,T,H,EQ>(RWTPtrHashMap<K,T,H,EQ>&h);
Creates an iterator for the hashed map h. The iterator begins in an undefined state and must be advanced before the first element will be accessible.
K* operator()();
Advances self to the next element, dereferences the resulting iterator and returns its key. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.
RWBoolean operator++();
Advances self to the next element. If the iterator has been reset or just created self will now reference the first element. If, before iteration, self referenced the last association in the multi-map, self will now reference an undefined value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned. Note: no post-increment operator is provided.
RWTPtrHashMap<K,T,H,EQ>* container() const;
Returns a pointer to the collection being iterated over.
K* key() const;
Returns the key portion of the association currently referenced by self. Undefined if self is not referencing a value within the map.
void reset(); void reset(RWTPtrHashMap<K,T,H,EQ>& h);
Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset() with no argument will reset the iterator on the current container. Supplying a hashed map with reset() will reset the iterator on that container.
T* value();
Returns the value portion of the association pointed to by self. The behavior is undefined if the map is empty.