《STL源码剖析》学习笔记(4)

20,STL仿函数需要定义相应的型别,以便让配接器取出,以此而拥有配接能力,相应型别只是一些typedef,所有操作在编译期全部完成,对程序的执行效率没有影响。仿函数的相应型别主要用来表现函数参数型别和传回值,为了方便,STL已经定义了两个class,代码如下:

template <class Arg, class Result>
struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
};
 
template </class><class Arg1, class Arg2, class Result>
struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};      
</class>


21,仿函数只是行为类似函数的对象,自定义了operator()操作符。以下为几个仿函数的代码:

template <class T>
struct logical_not : public unary_function<T, bool> {
    bool operator()(const T& x) const { return !x; }
};
 
template <class T>
struct plus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x + y; }
};

22,每一个fuction adapters内也有一个member object,其型别等同于它要配接的对象,以下为几个函数配接器的源码:

template <class Operation> 
class binder1st
  : public unary_function<typename Operation::second_argument_type,
                          typename Operation::result_type> {
protected:
  Operation op;
  typename Operation::first_argument_type value;
public:
  binder1st(const Operation& x,
            const typename Operation::first_argument_type& y)
      : op(x), value(y) {}
  typename Operation::result_type
  operator()(const typename Operation::second_argument_type& x) const {
    return op(value, x); 
  }
};
 
template <class Operation, class T>
inline binder1st<Operation> bind1st(const Operation& op, const T& x) {
  typedef typename Operation::first_argument_type arg1_type;
  return binder1st<Operation>(op, arg1_type(x));
}
 
 
//--------------------------------------
 
template <class Arg, class Result>
class pointer_to_unary_function : public unary_function<Arg, Result> {
protected:
  Result (*ptr)(Arg);
public:
  pointer_to_unary_function() {}
  explicit pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
  Result operator()(Arg x) const { return ptr(x); }
};
 
template <class Arg, class Result>
inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
  return pointer_to_unary_function<Arg, Result>(x);
}
 
template <class Arg1, class Arg2, class Result>
class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
protected:
    Result (*ptr)(Arg1, Arg2);
public:
    pointer_to_binary_function() {}
    explicit pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
    Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
};
 
template <class Arg1, class Arg2, class Result>
inline pointer_to_binary_function<Arg1, Arg2, Result> 
ptr_fun(Result (*x)(Arg1, Arg2)) {
  return pointer_to_binary_function<Arg1, Arg2, Result>(x);
}

23,一个使用mem_fun的示例代码:

class Shape {
public:
	virtual void draw() = 0;
};
 
class Circle : public Shape {
public:
	virtual void draw() { cout << "Circle::Draw()" << endl; }
};
 
class Square : public Shape {
public:
	virtual void draw() { cout << "Square::Draw()" << endl; }
};
 
int main() {
	vector<Shape*> vs;
	vs.push_back(new Circle);
	vs.push_back(new Square);
	for_each(vs.begin(), vs.end(), mem_fun(&Shape::draw));
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*