#include #include //for abs and EXIT_SUCCESS #include #include //for min_element #include //for ptr_fun #include //for compose1 #include "composer_fgx1_gx2.h" using namespace std; //Return true if the first argument is nearer to 32 than the second argument is. inline bool nearer_to_32_func(int a, int b) {return abs(a - 32) < abs(b - 32);} //Return true if the first argument is nearer to 32 than the second argument is. class nearer_to { const int n; public: nearer_to(int initial_n): n(initial_n) {} bool operator()(int a, int b) const {return abs(a - n) < abs(b - n);} }; int main() { const int a[] = {50, 10, 30, 35, 40}; const size_t n = sizeof a / sizeof a[0]; const int *const p = min_element(a, a + n); cout << "The smallest number in the array is " << *p << ".\n"; const vector v(a, a + n); vector::const_iterator it = min_element(v.begin(), v.end()); cout << "The smallest number in the vector is " << *it << ".\n"; it = min_element(v.begin(), v.end(), nearer_to_32_func); cout << "The number that's nearest to 32 is " << *it << ".\n"; it = min_element(v.begin(), v.end(), nearer_to(32)); cout << "The number that's nearest to 32 is " << *it << ".\n"; it = min_element(v.begin(), v.end(), compose_fgx1_gx2( less(), __gnu_cxx::compose1( ptr_fun(abs), bind2nd(minus(), 32)) ) ); cout << "The number that's nearest to 32 is " << *it << ".\n"; return EXIT_SUCCESS; }