03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.11. EXERCISES 131<br />

double operator() ( double x ) const {<br />

return std::exp( 3.0 ∗ x ) ;<br />

}<br />

} ;<br />

• f = sin(x) if x < 1 and f = cos(x) if x ≥ 1 <strong>for</strong> x ∈ [0, 4].<br />

• Can we use trapezoid( std::sin, 0.0, 2.0 ); ?<br />

As a second exercise, develop a functor <strong>for</strong> computing the finite difference. Then integrate the<br />

finite difference to verify that you get the function value back.<br />

4.11.11 STL and functor<br />

Write a generic function that copies the values of a container to another container after trans<strong>for</strong>mation<br />

using a functor:<br />

struct double functor {<br />

int operator() ( int v ) const {<br />

if (v my input vec ; ...<br />

std::vector< int > my output vec ;<br />

trans<strong>for</strong>m( my input vec.begin(), my input vec.end(), my output vec.begin(), double functor() ) ;<br />

Write code <strong>for</strong> the function trans<strong>for</strong>m and test it.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!