From fbfcf97e1888f5c2276109eeb4955d8b67dffa36 Mon Sep 17 00:00:00 2001
From: Jeremy Siek
The type Iterator must at least model Readable Iterator. The -resulting transform_iterator models the most refined of the +
The type Iterator must at least model Readable Iterator.
+The resulting transform_iterator models the most refined of the following options that is also modeled by Iterator.
@@ -104,8 +109,8 @@ concept that is modeled by Iterator result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type. The value_type is remove_cv<remove_reference<reference> >::type.
transform_iterator();
Iterator base() const;
+Returns: | m_iterator | +
---|
UnaryFunction functor() const;
typename transform_iterator::value_type dereference() const;
+reference operator*() const;
Returns: | m_f(transform_iterator::dereference()); | +
---|---|
Returns: | m_f(*m_iterator) | +
transform_iterator& operator++();
+Effects: | ++m_iterator | +
---|---|
Returns: | *this | +
+template <class UnaryFunction, class Iterator> +transform_iterator<UnaryFunction, Iterator> +make_transform_iterator(Iterator it, UnaryFunction fun); ++
Returns: | An instance of transform_iterator<UnaryFunction, Iterator> with m_f +initialized to f and m_iterator initialized to x. | +
---|
+template <class UnaryFunction, class Iterator> +transform_iterator<UnaryFunction, Iterator> +make_transform_iterator(Iterator it); ++
Returns: | An instance of transform_iterator with m_f +default constructed and m_iterator initialized to x. |
---|
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<int> > Function; +typedef boost::transform_iterator<Function, int*> doubling_iterator; + +doubling_iterator i(x, boost::bind1st(std::multiplies<int>(), 2)), + i_end(x + N, boost::bind1st(std::multiplies<int>(), 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<int>(), 4)), + boost::make_transform_iterator(x + N, boost::bind1st(std::plus<int>(), 4)), + std::ostream_iterator<int>(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 ++