From ba8dc44eac27eba5c4e30b34b391670f90ed7f2f Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 12 Jan 2004 15:20:01 +0000 Subject: [PATCH] new file [SVN r21627] --- doc/transform_iterator_eg.rst | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 doc/transform_iterator_eg.rst diff --git a/doc/transform_iterator_eg.rst b/doc/transform_iterator_eg.rst new file mode 100755 index 0000000..3b1403b --- /dev/null +++ b/doc/transform_iterator_eg.rst @@ -0,0 +1,37 @@ +Example +....... + +This is a simple example of using the transform_iterators class to +generate iterators that multiply (or add to) the value returned by +dereferencing the iterator. It would be cooler to use lambda library +in this example. + +:: + + int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + const int N = sizeof(x)/sizeof(int); + + typedef boost::binder1st< std::multiplies > Function; + typedef boost::transform_iterator doubling_iterator; + + doubling_iterator i(x, boost::bind1st(std::multiplies(), 2)), + i_end(x + N, boost::bind1st(std::multiplies(), 2)); + + std::cout << "multiplying the array by 2:" << std::endl; + while (i != i_end) + std::cout << *i++ << " "; + std::cout << std::endl; + + std::cout << "adding 4 to each element in the array:" << std::endl; + std::copy(boost::make_transform_iterator(x, boost::bind1st(std::plus(), 4)), + boost::make_transform_iterator(x + N, boost::bind1st(std::plus(), 4)), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + +The output is:: + + multiplying the array by 2: + 2 4 6 8 10 12 14 16 + adding 4 to each element in the array: + 5 6 7 8 9 10 11 12