polevoy_cpp_2012_spring_lecture_12
.pdf
std::unary_function
template <class Arg, class Res> struct unary_function
{
typedef Arg argument_type; typedef Res result_type;
};
28.04.2012 |
cppNewb.ru |
51 |
std::binary_function
template <class Arg1, class Arg2, class Res>
struct binary_function
{
typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Res result_type;
};
28.04.2012 |
cppNewb.ru |
52 |
std::less (пример функтора)
template<class T> struct less
: binary_function<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x < y;
}
};
28.04.2012 |
cppNewb.ru |
53 |
std::find_if (пример алгоритма)
template<class InputIt, class Predicate>
InputIt find_if(InputIt first, InputIt last, Predicate pred)
{
for(; first != last; ++first) if (pred(*first))
break; return first;
}
28.04.2012 |
cppNewb.ru |
54 |
Функциональные адаптеры
•указатель на функцию -> функтор pointer_to_binary_function, …
•связывание аргумента binder1st, binder2nd
•метод -> функтор mem_fun_t, …
•отрицание функтора
unary_negate, binary_negate
28.04.2012 |
cppNewb.ru |
55 |
stl::bind1st
template<class Op> class bind1st
:public unary_function<
typename Op::second_argument_type, typename Op::result_type>
{
protected: Op op;
typename Op::first_argument_type value;
...
28.04.2012 |
cppNewb.ru |
56 |
stl::bind1st
...
public:
bind1st(const Op& x,
const typename Op::first_argument_type& y) : op(x), value(y)
{}
typename Op::result_type operator()(
const typename Op::second_argument_type& x) const
{
return op(value,x);
}
};
28.04.2012 |
cppNewb.ru |
57 |
std::pointer_to_unary_function
template<class Arg, class Res> class pointer_to_unary_function
: public unary_function<Arg, Res>
{
protected:
Res(*pfunc)(Arg);
public:
explicit pointer_to_unary_function(Res(*f)(Arg)) : pfunc (f) {}
Res operator()(Arg x) const { return pfunc(x); }
};
28.04.2012 |
cppNewb.ru |
58 |
Производящая функция
•создание объекта заданного типа
•в шаблонном варианте автоматически
выводит тип объекта по типам
аргументов
28.04.2012 |
cppNewb.ru |
59 |
Производящие функции для функциональных адаптеров
•указатель на функцию -> функтор ptr_fun
•связывание аргумента bind1st, bind2nd
•метод -> функтор mem_fun, mem_fun_ref
•отрицание функтора not1, not2
28.04.2012 |
cppNewb.ru |
60 |
