Exercise 13:

Create a directory ex13 and save in this directory the files corresponding to your solutions to the following questions.

Question 1: Function objects (30 points).

This question will check your understanding of function objects.

Your goal is to write a function object Min_clamped that can be called as a function taking two arguments and returns the minimum between a member variable "_a" and the minimum of the two arguments. Min_clamped has one constructor:

Please create a file named "test_function_objects.cpp". Please type and complete the following code in this file:
// test_function_objects.cpp

#include <iostream>

template <class T>
class Min_clamped {
public:
 Min_clamped (const T& a = 0) /* complete the code for the constructor */

 // complete the class Min_clamped to have a function object

private:
 T _a;
};


int main() {
 Min_clamped<int> min_0;
 std::cout << min_0(-1,-2) << std::endl;
 std::cout << min_0(1,2)   << std::endl;
 
 Min_clamped<double> min_5(5.0);
 std::cout << min_5(-1.5, -2.5) << std::endl;
 std::cout << min_5(7.5, 7.0)  << std::endl;
}

Question 2: containers and iterators (40 points).

This exercise will check your understanding of containers and iterators in the standard library.

Your goal will be to write a template function my_find() taking as input an iterator pointing to the beginning of a container, an iterator pointing to the end of a container and an element 'e' and returning an iterator to the first found element in the container equal to 'e' or an iterator to the end of the container if no elements were found.
Please create a file "utils.h". Write your function template function my_find() in this file.

To test your code, please create a file "test_container_iterator.cpp" and type into it the following code:
#include "utils.h"
#include <list>
#include <iostream>

int main()
{
 std::list<int> l;
 l.push_back(1); l.push_back(2);
 l.push_back(5); l.push_back(1);
 l.push_back(2);

 std::list<int>::iterator it;
 it = my_find(l.begin(), l.end(), 2);
 if (it != l.end()) std::cout << "found " << *it << std::endl;
}

Question 3: iterators (30 points).

Write a program which reads integer numbers from std::cin using a std::istream_iterator. The program should print all even numbers, separated by spaces, to an output file. Use std::ostream_iterators for printing to the output file.