Вход/Регистрация
РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
вернуться

Менг Ли

Шрифт:

return !pred(x, y);

 }

};

template ‹class Predicate›

binary_negate‹Predicate› not2(const Predicate& pred) {

 return binary_negate‹Predicate›(pred);

}

Привязки (Binders)

Привязки bind1st и bind2nd берут функциональный объект f двух параметров и значение x и возвращают функциональный объект одного параметра, созданный из f с первым или вторым параметром соответственно, связанным с х.

template ‹class Predicate›

class binder1st: public unary_function {

protected:

 Operation op;

 Operation::first_argument_type value;

public:

 binder1st(const Operation& x, const Operation::first_argument_type& y) : op(x), value(y) {}

 result_type operator(const argument_type& x) const {

return op(value, x);

 }

};

template ‹class Operation, class T›

binder1st‹Operation› bind1st(const Operation& op, const T& x) {

 return binder1st‹Operation›(op, Operation::first_argument_type(x));

}

template ‹class Operation›

class binder2nd: public unary_function‹0peration::first_argument_type, Operation::result_type› {

protected:

 Operation op;

 Operation::second_argument_type value;

public:

 binder2nd(const Operation& x, const Operation::second_argument_type& y) : op(x), value(y) {}

 result_type operator(const argument_type& x) const {

return op(x, value);

 }

};

template ‹class Operation, class T›

binder2nd‹Operation› bind2nd(const Operation& op, const T& x) {

 return binder2nd‹0peration›(op, Operation::second_argument_type(x));

}

Например, find_if(v.begin, v.end, bind2nd(greater‹int›, 5)) находит первое целое число в векторе v большее, чем 5; find_if(v.begin, v.end, bind1st(greater‹int›, 5)) находит первое целое число в v меньшее, чем 5.

Адаптеры указателей на функции (Adaptors for pointers to functions)

Чтобы позволить указателям на (унарные и бинарные) функции работать с функциональными адаптерами, библиотека обеспечивает следующее:

template ‹class Arg, class Result›

class pointer_to_unary_function: public unary_function‹Arg, Result› {

protected:

 Result (*ptr)(Arg);

public:

 pointer_to_unary_function {}

 pointer_to_unary_function(Result (*x)(Arg)): ptr(x) {}

 Result operator(Arg x) const {return ptr(x);}

};

template ‹class Arg, class Result›

pointer_to_unary_function‹Arg, Result› ptr_fun(Result (*x)(Arg)) {

 return pointer_to_unary_function‹Arg, Result›(x);

}

template

class pointer_to_binary_function: public binary_function {

protected:

 Result (*ptr)(Arg1, Arg2);

public:

 pointer_to_binary_function {}

 pointer_to_binary_function(Result (*x)(Arg1, Arg2)): ptr(х) {}

 Result operator(Arg1 x, Arg2 y) const {return ptr(x, y);}

};

template ‹class Arg1, class Arg2, class Result›

pointer_to_binary_function‹Arg1, Arg2, Result› ptr_fun(Result (*x)(Arg1, Arg2)) {

 return pointer_to_binary_function‹Argl, Arg2, Result›(x);

}

Например, replace_if(v.begin, v.end, not1(bind2nd(ptr_fun(strcmp), "C")), "C++") заменяет все "С" на "C++" в последовательности v.

  • Читать дальше
  • 1
  • ...
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: