函数对象和谓词定义
函数对象:
重载函数调用操作符的类, 其对象常称为函数对象 (function object), 即它们是行为类似函数的对象. 一个类对象, 表现出一个函数的特征, 就是通过 "对象名 +(参数列表 http://baike.baidu.com/view/8423750.htm )" 的方式使用一个类对象, 如果没有上下文, 完全可以把它看作一个函数对待.
这是通过重载 http://baike.baidu.com/view/126530.htm 类的 operator() 来实现的.
"在标准库中, 函数对象被广泛地使用以获得弹性", 标准库中的很多算法都可以使用函数对象或者函数来作为自定的回调行为;
谓词:
一元函数对象: 函数参数 1 个;
二元函数对象: 函数参数 2 个;
一元谓词 函数参数 1 个, 函数返回值是 bool 类型, 可以作为一个判断式
谓词可以使一个仿函数, 也可以是一个回调函数.
二元谓词 函数参数 2 个, 函数返回值是 bool 类型
一元谓词函数举例如下
1, 判断给出的 string 对象的长度是否小于 6
- bool GT6(const string &s)
- {
- return s.size()>= 6;
- }
2, 判断给出的 int 是否在 3 到 8 之间
- bool Compare( int i )
- {
- return ( i>= 3 && i <= 8 );
- }
二元谓词举例如下
1, 比较两个 string 对象, 返回一个 bool 值, 指出第一个 string 是否比第二个短
- bool isShorter(const string &s1, const string &s2)
- {
- return s1.size() <s2.size();
- }
一元函数对象例子:
- //1 普通类 重载 函数调用操作符
- template <typename T>
- void FuncShowElemt(T &t) // 普通函数 不能像 仿函数那样记录状态
- {
- cout <<t << " ";
- };
- void showChar(char &t)
- {
- cout << t << " ";
- }
- // 函数模板 重载 函数调用操作符
- template <typename T>
- class ShowElemt
- {
- public:
- ShowElemt()
- {
- n = 0;
- }
- void operator()(T &t)
- {
- n++;
- cout <<t << " ";
- }
- void printCount()
- {
- cout << n << endl;
- }
- public:
- int n;
- };
- //1 函数对象 基本使用
- void main()
- {
- int a = 100;
- FuncShowElemt<int>(a); // 普通的函数调用
- ShowElemt<int> showElemt; // 函数对象
- showElemt(a); // 函数对象调用
- }
一元谓词例子:
- //1 元谓词 例子
- template <typename T>
- class Isdiv
- {
- public:
- Isdiv(const T &divisor) //
- {
- this->divisor = divisor;
- }
- bool operator()(T &t)
- {
- return (t%divisor == 0);
- }
- protected:
- private:
- T divisor;
- };
- void main()
- {
- vector<int> v2;
- for (int i=10; i<33; i++)
- {
- v2.push_back(i);
- }
- vector<int>::iterator it;
- int a = 4;
- Isdiv<int> mydiv(a);
- // _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred) // 返回的是迭代器
- it = find_if(v2.begin(), v2.end(), Isdiv<int>(4));
- if (it != v2.end())
- {
- cout <<"第一个被 4 整除的数是:" << *it << endl;
- }
- }
二元函数对象例子:
- template <typename T>
- struct SumAdd
- {
- T operator()(T &t1, T &t2)
- {
- return t1 + t2;
- }
- };
- template <typename T>
- void printE(T &t)
- {
- for (vector<int>::iterator it = t.begin(); it!=t.end(); it++ )
- {
- cout <<*it << " ";
- }
- }
- void printVector(vector<int> &v)
- {
- for (vector<int>::iterator it = v.begin(); it!=v.end(); it++ )
- {
- cout <<*it << " ";
- }
- }
- void main()
- {
- vector<int> v1, v2 ;
- vector<int> v3;
- v1.push_back(1);
- v1.push_back(2);
- v1.push_back(3);
- v2.push_back(4);
- v2.push_back(5);
- v2.push_back(6);
- v3.resize(10);
- //transform(v1.begin(), v1.end(), v2.begin(),v3.begin(), SumAdd<int>());
- /*
- template<class _InIt1,
- class _InIt2,
- class _OutIt,
- class _Fn2> inline
- _OutIt transform(_InIt1 _First1, _InIt1 _Last1,
- _InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
- */
- vector<int>::iterator it = transform(v1.begin(), v1.end(), v2.begin(),v3.begin(), SumAdd<int>());
- cout <<*it << endl;
- printE(v3);
- }
二元谓词例子:
- void current(int &v)
- {
- cout << v << " ";
- }
- bool MyCompare(const int &a, const int &b)
- {
- return a < b;
- }
- void main()
- {
- vector<int> v(10);
- for (int i=0; i<10; i++)
- {
- v[i] = rand() % 100;
- }
- for_each(v.begin(), v.end(), current);
- printf("\n");
- sort(v.begin(), v.end(), MyCompare );
- printf("\n");
- for (int i=0; i<10; i++)
- {
- printf("%d", v[i]);
- }
- printf("\n");
- }
来源: http://www.bubuko.com/infodetail-2806048.html