diff --git a/reverse_iterator.htm b/reverse_iterator.htm new file mode 100644 index 0000000..50d41a7 --- /dev/null +++ b/reverse_iterator.htm @@ -0,0 +1,232 @@ + + +
+ + + ++The reverse iterator adaptor flips the direction of an iterators +motion. Invoking operator++() moves the reverse iterator +backwards and calling operator--() moves the reverse iterator +forwards. The Boost reverse iterator adaptor is better to use than the +std::reverse_iterator class in situations where pairs of +mutable/const iterators are needed (such as in containers) because +comparisons and conversions between the mutable and const versions are +implemented correctly. + +
+namespace boost { + template <class BidirectionalIterator, + class Value, class Reference, class Pointer, class Category, class Distance> + struct reverse_iterator_generator; + + template <class BidirectionalIterator> + typename reverse_iterator_generator<BidirectionalIterator>::type + make_reverse_iterator(BidirectionalIterator base) +} ++ +
+template <class BidirectionalIterator, + class Value, class Reference, class Pointer, class Category, class Distance> +class reverse_iterator_generator +{ +public: + typedef iterator_adaptor<...> type; // the resulting reverse iterator type +}; ++ +
+#include <boost/config.hpp> +#include <iostream> +#include <algorithm> +#include <boost/iterator_adaptors.hpp> + +int main(int, char*[]) +{ + char letters[] = "hello world!"; + const int N = sizeof(letters)/sizeof(char) - 1; + std::cout << "original sequence of letters:\t" + << letters << std::endl; + + std::sort(letters, letters + N); + + // Use reverse_iterator_generator to print a sequence + // of letters in reverse order. + + boost::reverse_iterator_generator<char*>::type + reverse_letters_first(letters + N), + reverse_letters_last(letters); + + std::cout << "letters in descending order:\t"; + std::copy(reverse_letters_first, reverse_letters_last, + std::ostream_iterator<char>(std::cout)); + std::cout << std::endl; + + // to be continued... ++The output is: +
+original sequence of letters: hello world! +letters in descending order: wroolllhed! ++ +
Parameter | Description | +
---|---|
BidirectionalIterator | +The iterator type being wrapped. | + +
Value | +The value-type of the base iterator and the resulting reverse iterator. +Default:std::iterator_traits<BidirectionalIterator>::value_type + |
+
+
Reference | +The corresponding reference type for the value_type. +Default:std::iterator_traits<BidirectionalIterator>::reference + |
+
+
Pointer | +The pointer type for the value_type. +Default:std::iterator_traits<BidirectionalIterator>::pointer + |
+
+
Category | +The iterator category. +Default:std::iterator_traits<BidirectionalIterator>::iterator_category + |
+
+
Distance | +The corresponding pointer type for the Value. +Default:std::iterator_traits<BidirectionalIterator>::difference_type + |
+
+
+
+reverse_iterator_generator::type(const BidirectionalIterator& it) ++ +
+
+ +
+ template <class BidirectionalIterator> + typename reverse_iterator_generator<BidirectionalIterator>::type + make_reverse_iterator(BidirectionalIterator base) ++ + +
+ // continuing from the previous example... + + std::cout << "letters in ascending order:\t"; + std::copy(boost::make_reverse_iterator(reverse_letters_last), + boost::make_reverse_iterator(reverse_letters_first), + std::ostream_iterator+The output is: +(std::cout)); + std::cout << std::endl; + + return 0; +} +
+letters in ascending order: !dehllloorw ++ +
Revised 10 Feb 2001
+© Copyright Jeremy Siek 2000. Permission to copy, use, +modify, sell and distribute this document is granted provided this copyright +notice appears in all copies. This document is provided "as is" +without express or implied warranty, and with no claim as to its suitability for +any purpose.
+ + + + + + + diff --git a/reverse_iterator_example.cpp b/reverse_iterator_example.cpp new file mode 100644 index 0000000..40c859f --- /dev/null +++ b/reverse_iterator_example.cpp @@ -0,0 +1,42 @@ +// (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +#include