STL 函数对象

函数符
******************************
******************************
生成器(generator)不用参数就可以调用的函数符
一元函数(unary funcation)
二元函数(binary funcation)
返回bool值的一元函数是断言(predicate)
返回bool值的一元函数是二元断言(predicate)
////////////////////////////////////////////////////////////
//
// functor.cpp --- using a functor
//
////////////////////////////////////////////////////////////
#include <iostream>
#include <list>
#include <iterator>

template<class T>  // functor class defines operator()()
class TooBig
{
private:
    T cutoff;
public:
    TooBig(const T & t) : cutoff(t) {}
    bool operator()(const T & v) { return v > cutoff; }//重载()操作符
};

int main()
{
    using std::list;
    using std::cout;
    using std::endl;

    TooBig<int> f100(100); // limit = 100
    list<int> yadayada;
    list<int> etcetera;
    int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};

    yadayada.insert(yadayada.begin(), vals, vals + 10);
    etcetera.insert(etcetera.begin(), vals, vals + 10);
    std::ostream_iterator<int, char> out(cout, " ");
    cout << "Original lists:\n";
    copy(yadayada.begin(), yadayada.end(), out);
    cout << endl;
    copy(etcetera.begin(), etcetera.end(), out);
    cout << endl;
    yadayada.remove_if(f100);               // use a named function object
    etcetera.remove_if(TooBig<int>(200));   // construct a function object   remove_if()
    cout <<"Trimmed lists:\n";
    copy(yadayada.begin(), yadayada.end(), out);
    cout << endl;
    copy(etcetera.begin(), etcetera.end(), out);
    cout << endl;

    return 0;
}

*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
假设有接受两个参数的模板函数
template <class T>
bool tooBig(const T & val,const T & lim)
{
    return val>lim;
}
使用类将其转换为接受单个参数的函数对象
template<class T>
class tooBig2
{
private:
   T cutoff;
public:
   tooBig2(const T & t);cutoff(t) {}
   bool operator() (const T & v) {return tooBig<T>(v,vutoff);}
};
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

 


预定义函数符
**********************************************
**********************************************
#include <functional>
+      plus                   >    greater
-      minus                  <    less
*      multiplies            >=    greater_equal
/      divides               <=    less_equal
%      modulus               &&    logical_and
-      negate                ||    logical_or
==     equal_to               !    logical_not
!=     not_equal_to

算法函数 transform()
版本1 :
transform(gr8.begin(),gr8.end(),out,sqrt)
//(把gr8这个容器对象中的值给 函数对象sqrt处理 然后发送到out迭代输出流) 
版本2 :
transform(gr8.begin(),gr8.end(), new.begin(),out,plus<int>())
(把gr8这个容器对象中的值和new对象中值 给函数对象add处理 然后发送到out迭代输出流)

 

自适应函数符 函数适配器
********************************************
********************************************
二元函数转一元接口
binder1st  bind1st  binder2nd  bind2nd
ep:
f2()为一个自适应2元函数
调用 binder1st(f2,val) f1;
则  f1(x)  等价与 f2(val,x);  //val做第一个参数

multi()2元函数
同样 bind1st(multi<double>(),2.5)

binder2nd  bind2nd类似 不同是参数被传到第2个参数的位子

// funadap.cpp -- using function adapters
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

void Show(double);
const int LIM = 5;
int main()
{
    using namespace std;
    double arr1[LIM] = {36, 39, 42, 45, 48};
    double arr2[LIM] = {25, 27, 29, 31, 33};
    vector<double> gr8(arr1, arr1 + LIM);
    vector<double> m8(arr2, arr2 + LIM);
    cout.setf(ios_base::fixed);
    cout.precision(1);
    cout << "gr8:\t";
    for_each(gr8.begin(), gr8.end(), Show);
    cout << endl;
    cout << "m8: \t";
    for_each(m8.begin(), m8.end(), Show);
    cout << endl;

    vector<double> sum(LIM);
    transform(gr8.begin(), gr8.end(), m8.begin(), sum.begin(),
              plus<double>());
    cout << "sum:\t";
    for_each(sum.begin(), sum.end(), Show);
    cout << endl;

    vector<double> prod(LIM);
    transform(gr8.begin(), gr8.end(), prod.begin(),
              bind1st(multiplies<double>(), 2.5));
    cout << "prod:\t";
    for_each(prod.begin(), prod.end(), Show);
    cout << endl;

    return 0;
}

void Show(double v)
{
    std::cout.width(6);
    std::cout << v << ' ';
}

posted @ 2007-03-18 17:00  Edward Xie  阅读(1293)  评论(0)    收藏  举报