[Home]CPPTM Answers - Exercise 9-2

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

 // (by Ariel Badichi)
 #include <iostream>
 #include <iterator>
 #include <functional>
 #include <cmath>
 #include <algorithm>

 template<typename R, typename F, F f, typename G, G g>
 struct compose_fg2
 {
     typedef R result_type;

     template<typename T>
     R operator() (const T & x) const
     {
         return f(g(x));
     }
 };

 struct log2
 {
     float operator() (float x) const { return std::log(x) / std::log(2.0f); }
 };

 struct sin_squared
 {
     float operator() (float x) const { return std::sin(std::sin(x)); }
 };

 namespace
 {
     log2 log2_s;
     sin_squared sin_squared_s;
 }

 int main()
 {
     const float input[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f};
     float output[5] = {0};

     std::transform(input, input + 5, output, 
                    compose_fg2<
                        float, 
                        sin_squared &, 
                        sin_squared_s, 
                        log2 &, 
                        log2_s
                    >());

     std::ostream_iterator<float> out(std::cout, "\n");
     std::copy(output, output + 5, out);

     return 0;
 }

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited March 27, 2005 12:28 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers