C++标准库---ptr_fun()
来源:互联网 发布:国网网络大学考试烦人 编辑:程序博客网 时间:2024/06/18 18:34
ptr_fun是将一个普通的函数适配成一个仿函数(functor), 添加上argument_type和result type等类型.
例如:
#include <algorithm> #include <functional> #include <iostream> using namespace std; int sum(int arg1, int arg2) { std::cout<< "arg1 = " << arg1 << std::endl; std::cout<< "arg2 = " << arg2 << std::endl; int sum = arg1 + arg2; std::cout << "sum = " << sum << std::endl; return sum; }int main(int argc, char *argv[], char *env[]){ bind1st(ptr_fun(sum), 1)(2);// the same as sum(1,2) bind2nd(ptr_fun(sum), 1)(2);// the same as sum(2,1) system("pause");return 0;}
运行结果:
代码示例:
#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<string>using namespace std;int main(){vector<char*> coll;vector<char*>::iterator iter1,iter2;coll.push_back("lan");coll.push_back("zhi");coll.push_back("hui");coll.push_back("is");coll.push_back("a");coll.push_back("good");coll.push_back("boy!");for(iter1=coll.begin();iter1!=coll.end();++iter1){cout<<*iter1<<" ";}cout<<endl;iter2=find_if(coll.begin(),coll.end(),not1(bind2nd(ptr_fun(strcmp),"hui")));//hui与coll中所有字符串比较,如果相同,则反相后输出if(iter2!=coll.end()){cout<<"Found: "<<*iter2<<endl;}else{cout<<"Not Found"<<endl;}system("pause");return 0;}运行结果:
lanzhihui is a good boy
Found: hui
0 0
- C++标准库---ptr_fun()
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
- ptr_fun
- ptr_fun
- ptr_fun
- C标准库“圣经”:C标准库
- ptr_fun详解
- ptr_fun详解
- 标准c++ 库
- C标准库[转]
- C标准库
- (不错)C标准库
- C标准库
- C语言标准库
- C标准库“圣经”
- 解读《C标准库》
- linux命令选项 英文
- Linux常用C函数---字符测试篇
- How to Use beEF (Browser Exploitation Framework)
- DG备库磁盘空间满导致无法创建归档
- 《大数据时代》第四章读后整理和感悟
- C++标准库---ptr_fun()
- Ubuntu安装NS-3方法和遇到的问题
- 统计单词个数
- 加密和认证
- 重写 log4j RollingFileAppender,支持每日归档
- 条件语句和linux下c编程初体验
- html 如何让微信里的html应用弹出“点击右上角分享到朋友圈”的图片
- 远程使用数据库
- 坐标和依赖