Tree Container Library: Reverse Element Iterators
tree

The Tree Container Library

A C++ generic tree container library which, not only works much like the STL, but is also compatible with the STL algorithms. The Tree Container Library is used worldwide in thousands of industries and organizations. It is completely free to use, and is licensed under the zlib/libpng license.

Reverse child element iterators, reverse_iterator, and const_reverse_iterator are available to traverse children in a reverse order. Reverse child element iterators, described on this page, expose the underlying element. This is opposed to the reverse child node iterators, described here, which expose the underlying node. Although, this page describes the reverse child element iterators, this discussion also applies to the it's node counterpart reverse iterators. These reverse iterators work much like the reverse iterators in the STL. Accompanied reverse element iterator accessors are available for all four tree containers, which are listed below.

  • reverse_iterator rbegin()
    const_reverse_iterator rbegin() const
    Returns a reverse_iterator (or const_reverse_iterator in the second variety) which exposes the last child node of the node which the operation is called. If there are no children in the node which the operation is called, rend() is returned.
  • reverse_iterator rend()
    const_reverse_iterator rend()
    Returns a reverse_iterator (or const_reverse_iterator in the second variety) which exposes one node prior to the first child node, or the open reverse end.

Reverse child iterators are implemented much like the reverse iterators of the STL. Normal forward iterators have a range of [first node, last node) and reverse iterators have a range of (last node, first node]. As you can see, the normal iterators have a logical closed range on the left, and an open range on the right, whereas reverse iterators have an logical open range on the left, and closed range at right. For implementation purposes, the reverse iterators are implemented to have a range of [last node, first node), or, an open range on the left and a closed range on the right, like the normal iterators. To perform this, the reverse iterators actually point to one node past the node they expose, but return the value of the node immediately to their left. So, a reverse iterator with the value of rbegin() actually points the the same location as the the regular iterators end() position. All this is transparent to the user, but it is good to know when converting between regular and reverse iterators.

When converting between forward and reverse iterators, the underlying node will be the same, but the exposed node (node which is accessed by * and -> is different). Reverse iterators and iterators can be converted to the other in two different ways.

  • Converting an iterator to a reverse iterator.
    An iterator can be converted to a reverse iterator only on construction, with the reverse iterator taking the iterator as an argument in it's constructor.
  • Converting a reverse iterator to an iterator.
    A reverse iterator can be converted to an iterator by using the base() operation of the reverse iterator.
    eg: iter = reverse_iter.base();

Reverse child element iterators have the same interface (except for the ++ and -- operators working in reverse) as the regular child element iterators.