Чтение онлайн

ЖАНРЫ

РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)

Менг Ли

Шрифт:

 }

 back_insert_iterator‹Container›& operator* {return *this;}

 back_insert_iterator‹Container›& operator++ {return *this;}

 back_insert_iterator‹Container›& operator++(int) {return *this;}

};

template ‹class Container›

back_insert_iterator‹Container› back_inserter(Container& x) {

 return back_insert_iterator‹Container›(x);

}

template ‹class Container›

class front_insert_iterator: public output_iterator {

protected:

 Container& container;

public:

 front_insert_iterator(Container& x): container (x) {}

 front_insert_iterator‹Container›& operator=(const Container::value_type& value) {

container.push_front(value);

return *this;

 }

 front_insert_iterator‹Container›& operator* {return *this;}

 front_insert_iterator‹Container›& operator++ {return *this;}

 front_insert_iterator‹Container›& operator++(int) {return *this;}

};

template ‹class Container›

front_insert_iterator‹Container› front_inserter(Container& x) {

 return front_insert_iterator‹Container›(х);

}

template ‹class Container›

class insert_iterator: public output_iterator {

protected:

 Container& container;

 Container::iterator iter;

public:

 insert_iterator(Container& x, Container::iterator i) : container (x), iter(i) {}

 insert_iterator‹Container›& operator=(const Container::value_type& value) {

iter = container.insert(iter, value);

++iter;

return *this;

 }

 insert_iterator‹Container›& operator* {return *this;}

 insert_iterator‹Container›& operator++ {return *this;}

 insert_iterator‹Container›& operator++(int) {return *this;}

};

template ‹class Container, class Iterator›

insert_iterator<Container› inserter(Container& x, Iterator i) {

 return insert_iterator‹Container›(x, Container::iterator(i));

}

Адаптеры

функций (Function adaptors)

Функциональные адаптеры работают только с классами функциональных объектов с определёнными типами параметров и типом результата.

Отрицатели (Negators)

Отрицатели not1 и not2 берут

унарный и бинарный предикаты соответственно и возвращают их дополнения.

template ‹class Predicate›

class unary_negate: public unary_function‹Predicate::argument_type, bool› {

protected:

 Predicate pred;

public:

 unary_negate(const Predicate& x): pred(x) {}

 bool operator(const argument_type& x) const {return !pred(x);}

};

template ‹class Predicate›

unary_negate‹Predicate› not1(const Predicate& pred) {

 return unary_negate‹Predicate›(pred);

}

template ‹class Predicate›

class binary_negate: public binary_function‹Predicate::first_argument_type, Predicate::second_argument_type, bool› {

protected:

 Predicate pred;

public:

 binary_negate(const Predicate& x): pred(x) {}

 bool operator(const first_argument_type& x, const second_argument_type& y) const {

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:

Поделиться с друзьями: