Outline
Purpose
Provide basic iterator traits, adaptors, and utilities.
Classes
- bsl::iterator_traits: information about iterator associated types
- bsl::reverse_iterator: bring in
std::reverse_iterator
- bsl::distance: global function to calculate iterator distance
Canonical header: bsl_iterator.h
- See also
- bslstl_forwarditerator, bslstl_bidirectionaliterator, bslstl_randomaccessiterator, C++ Standard
Description
This component is for internal use only. Please include <bsl_iterator.h>
directly. This component provides the facilities of the iterators library from the C++ Standard, including iterator primitives (24.4), iterator adaptors (24.5), and stream iterators (24.6).
Usage
In this section we show intended use of this component.
Example 1: Using Iterators to Traverse a Container
In this example, we will use the bsl::iterator
and bsl::reverse_iterator
to traverse an iterable container type.
Suppose that we have an iterable container template type MyFixedSizeArray
. An instantiation of MyFixedSizeArray
represents an array having fixed number of elements, which is a parameter passed to the class constructor during construction. A traversal of MyFixedSizeArray
can be accomplished using basic iterators (pointers) as well as reverse iterators.
First, we create a elided definition of the template container class, MyFixedSizeArray
, which provides mutable and constant iterators of template type bsl::iterator
and reverse_iterator :
template <class VALUE, int SIZE>
class MyFixedSizeArray
{
VALUE d_array[SIZE];
public:
typedef VALUE value_type;
Here, we define mutable and constant iterators and reverse iterators:
typedef VALUE *iterator;
typedef VALUE const *const_iterator;
typedef bsl::reverse_iterator<iterator> reverse_iterator;
typedef bsl::reverse_iterator<const_iterator> const_reverse_iterator;
Now, we define the begin
and end
methods to return basic iterators (VALUE*
and const VALUE*
), and the rbegin
and rend
methods to return reverse iterators (bsl::reverse_iterator<VALUE*>
and bsl::reverse_iterator<const VALUE*>)
type:
iterator begin();
iterator end();
reverse_iterator rbegin();
reverse_iterator rend();
VALUE& operator[](int i);
const_iterator begin() const;
const_iterator end() const;
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
int size() const;
const VALUE& operator[](int i) const;
};
Then, we create a MyFixedSizeArray
and initialize its elements:
MyFixedSizeArray<int, 5> fixedArray;
for (int i = 0; i < fixedArray.size(); ++i) {
fixedArray[i] = i + 1;
}
Next, we generate reverse iterators using the rbegin
and rend
methods of the fixed array object:
MyFixedSizeArray<int, 5>::reverse_iterator rstart = fixedArray.rbegin();
MyFixedSizeArray<int, 5>::reverse_iterator rfinish = fixedArray.rend();
Now, we note that we could have acquired the iterators and container size by calling the appropriate free functions:
assert(fixedArray.size() ==
bsl::size(fixedArray));
assert(rfinish - rstart ==
bsl::ssize(fixedArray));
T::reverse_iterator rend(T &container)
Definition bslstl_iterator.h:1625
T::reverse_iterator rbegin(T &container)
Definition bslstl_iterator.h:1567
BSLS_KEYWORD_CONSTEXPR std::ptrdiff_t ssize(const TYPE(&)[DIMENSION]) BSLS_KEYWORD_NOEXCEPT
Return the dimension of the specified array argument.
Definition bslstl_iterator.h:1394
BSLS_KEYWORD_CONSTEXPR size_t size(const TYPE(&)[DIMENSION]) BSLS_KEYWORD_NOEXCEPT
Return the dimension of the specified array argument.
Definition bslstl_iterator.h:1331
Finally, we traverse the fixed array again in reverse order using the two generated reverse iterators:
printf("Traverse array using reverse iterator:\n");
while (rstart != rfinish) {
printf("\tElement: %d\n", *rstart);
++rstart;
}
The preceding loop produces the following output on stdout
:
Traverse array using reverse iterator:
Element: 5
Element: 4
Element: 3
Element: 2
Element: 1