|
BDE 4.14.0 Production release
|
Provide a template to create STL-compliant bidirectional iterators.
Canonical header: bsl_iterator.h
This component provides an iterator adaptor that, given an implementation class defining a core set of iterator functionality specified in the class level documentation, adapts it to provide an STL-compliant bidirectional iterator interface. bslstl::BidirectionalIterator meets the requirements of a bidirectional iterator described in the C++11 standard [24.2.7] under the tag "[bidirectional.iterators]". Include bsl_iterator.h to use this component.
In this section we show intended use of this component.
Suppose we want to create a standard compliant bidirectional access iterator for a container.
First, we define an iterator, MyArrayIterator, that meets the requirements of the IMP_ITER template parameter of BidirectionalIterator class (see class level documentation), but does not meet the full set of requirements for a bidirectional iterator as defined by the C++ standard. Note that the following shows only the public interface required. Private members and additional methods that may be needed to implement this class are elided in this example:
Notice that MyArrayIterator does not implement a complete standard compliant bidirectional iterator. It is missing methods such as operator+ and operator[].
Then, we define the interface for our container class template, MyFixedSizeArray. The implementation of the interface is elided for brevity:
Now, we use BidirectionalIterator to create a standard compliant iterator for this container:
Notice that the implementation for const_iterator is MyArrayIterator<VALUE> and not MyArrayIterator<const VALUE>.
Next, we continue defining the rest of the class.
Then, we create a MyFixedSizeArray and initialize its elements:
Finally, to show that MyFixedSizeArray::iterator can be used as a bidirectional iterator, we invoke a function that takes bidirectional iterators as parameters, such as std::reverse, on the begin and end iterators and verify the results: