博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ STL 之 函数对象适配器
阅读量:4505 次
发布时间:2019-06-08

本文共 3392 字,大约阅读时间需要 11 分钟。

谓词是指普通函数或重载的 operator()返回值是 bool 类型的函数对象(仿函数)。如果operator 接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。

例如:

struct myfuncobj01{  bool operator(int v){} // 接受一个参数,并且返回值为 Bool 即一元谓词}bool compare01(int v); // 同样是叫做一元谓词struct myfuncobj02{  bool operator(int v1,int v2){} // 接受两个参数,返回值为 Bool 即二元谓词}bool compare02(int v1,int v2); // 同样是叫做二元谓词

函数对象适配器是完成一些配接工作,这些配接包括绑定(bind),否定(negate),以及对一般函数或成员函数的修饰,使其成为函数对象。

如果希望函数对象适配器能对我们自己编写的函数对象有效,我们需要根据我们的函数对象类型继承 STL 的父类对象。
如果你本身是二元函数对象 需要继承
二元函数继承:public binary_function<参数类型,参数类型,返回类型>
一元函数继承:public unary_function<参数类型,返回类型>

#include 
#include
#include
#include
using namespace std;struct MyPrint : public binary_function
{ void operator()(int v, int val) const { cout << "v = " << v << " val = " << val << endl; cout << "v + val = " << v + val << endl; } };// 仿函数适配器 bind1st bind2nd 绑定适配器void test01(){ vector
v; for (int i = 0; i < 10; i++) { v.push_back(i); } int addNum = 200; for_each(v.begin(), v.end(), bind2nd(MyPrint(), addNum)); // 绑定适配器 将一个二元函数对象转变成一元函数对象 // bind1st bind2nd区别? // bind1st,将addNum绑定为函数对象的第一个参数 // bind2nd,将addNum绑定为函数对象的第二个参数 cout << "----------------------" << endl;}struct MyPrint02{ void operator()(int v) { cout << v << " "; }};struct MyCompare : public binary_function
{ bool operator()(int v1, int v2) const { return v1 > v2; }};struct MyGreater5 : public binary_function
{ bool operator()(int v, int val) const { cout << "v = " << v << " val = " << val << endl; return v > val; }};// 仿函数适配器 not1 not2 取反适配器void test02(){ vector
v; for (int i = 0; i < 10; i++) { v.push_back(i); } for_each(v.begin(), v.end(), MyPrint02()); cout << endl; // 正常排序 从大到小 sort(v.begin(), v.end(), MyCompare()); for_each(v.begin(), v.end(), MyPrint02()); cout << endl; // 取反之后从小到大 sort(v.begin(), v.end(), not2(MyCompare())); for_each(v.begin(), v.end(), MyPrint02()); cout << endl; // not1 not2 // 如果对二元谓词取反,用not2 // 如果对一元谓词取反,用not1 vector
::iterator it = find_if(v.begin(), v.end(), not1(bind2nd(MyGreater5(), 11))); if (it == v.end()) { cout << "没有找到!" << endl; } else { cout << *it << endl; } cout << "----------------------" << endl;}// 仿函数适配器 ptr_funvoid MyPrint03(int val, int val2){ cout << "val = " << val << " val2 = " << val2 << endl; cout << "val + val2 = " << val + val2 << endl;}void test03(){ vector
v; for (int i = 0; i < 10; i++) { v.push_back(i); } // ptr_func把普通函数转成函数对象 for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint03), 10)); cout << "--------------" << endl;}// 成员函数适配器 mem_fun mem_fun_refclass Person{public: Person(int age, int id) : age(age), id(id){} void show() { cout << "age = " << age << " id = " << id << endl; }public: int age; int id;};void test04(){ // 如果容器中存放的对象或者对象指针,我们for_each算法打印的时候,调用类自己提供的打印函数 vector
v; Person p1(10, 20), p2(30, 40), p3(50, 60); v.push_back(p1); v.push_back(p2); v.push_back(p3); for_each(v.begin(), v.end(), mem_fun_ref(&Person::show)); cout << "--------------" << endl; vector
v1; v1.push_back(&p1); v1.push_back(&p2); v1.push_back(&p3); for_each(v1.begin(), v1.end(), mem_fun(&Person::show)); // mem_fun_ref mem_fun区别? // 如果存放的是对象指针 使用mem_fun // 如果使用的是对象 使用mem_fun_ref}int main(){ test01(); test02(); test03(); test04(); getchar(); return 0;}

 

转载于:https://www.cnblogs.com/duxie/p/10939942.html

你可能感兴趣的文章
使用angular.js获取form表单中的信息
查看>>
TestNG
查看>>
高精——模板
查看>>
生成CFree 5.0 注册码
查看>>
磁力链接
查看>>
【问题解决方案】之 关于某江加密视频swf专用播放器仍无法播放的问题
查看>>
2014,码农梦想,先从态度开始!
查看>>
常用板子
查看>>
linux中安装eclipse--CnetOS6.5
查看>>
应用层拒绝服务攻击
查看>>
JavaScript学习总结(五)——jQuery插件开发与发布
查看>>
广度优先(迷宫找人)
查看>>
word2vec 评测 window_different
查看>>
我觉得二专很OK-2
查看>>
poj 2777
查看>>
最新版本GIT安装
查看>>
Python微信
查看>>
Oracle 存储过程起步
查看>>
python变量和作用域
查看>>
将AJAX返回值纵向排序赋值给Table标签
查看>>