使用boost的bind
来源:互联网 发布:suse防火墙开放端口 编辑:程序博客网 时间:2024/05/17 21:56
#include <algorithm>#include <map>#include <iostream>#include "boost/bind.hpp"using namespace std;void printPair(const pair<int, int>& elem){ cout << elem.first << "->" << elem.second << endl;}class Test {public:Test() { myMap.insert(make_pair(4, 40)); myMap.insert(make_pair(5, 50)); myMap.insert(make_pair(6, 60)); myMap.insert(make_pair(7, 70)); myMap.insert(make_pair(8, 80));}void printResult() { int a = 10; //using static method with two argument for_each(myMap.begin(), myMap.end(), boost::bind(&Test::printPair, _1, a)); //using static method with one argument for_each(myMap.begin(), myMap.end(), boost::bind(&Test::printPair2, _1)); //using instance method for_each(myMap.begin(), myMap.end(), boost::bind(&Test::printPair3, boost::ref(*this), _1)); }static void printPair(const pair<int, int>& elem, int a){ cout << elem.first << "->" << elem.second << ";a=" << a << endl;}static void printPair2(const pair<int, int>& elem){ cout << elem.first << "2->" << elem.second << endl;}void printPair3(const pair<int, int>& elem){ cout << elem.first << "3->" << elem.second << endl;}private: map<int, int> myMap;};int f(int a, int b) { return a - b;}int main(int argc, char** argv){ map<int, int> myMap; myMap.insert(make_pair(4, 40)); myMap.insert(make_pair(5, 50)); myMap.insert(make_pair(6, 60)); myMap.insert(make_pair(7, 70)); myMap.insert(make_pair(8, 80)); for_each(myMap.begin(), myMap.end(), &printPair); Test t; t.printResult(); //using bind create a new method then call it with 1 int c = boost::bind(f, _1, 2)(1); std::cout << "c:" << c << endl; c = boost::bind(f, 10, _1)(1); std::cout << "c:" << c << endl; //exchange the argument c = boost::bind(f, _2, _1)(1, 2); std::cout << "c:" << c << endl; return (0);}
0 0
- 【boost】boost::bind的使用
- boost::bind的使用
- boost bind的使用
- boost bind的使用
- Boost.bind 的使用
- boost::bind的使用
- 使用boost的bind
- boost的bind使用感受
- Boost.Bind的基础使用
- Boost.Bind的基础使用
- boost中bind的使用
- boost之bind的使用
- boost中bind的使用
- boost::function/bind 的使用
- boost::function与boost::bind 的使用
- [转]Boost.Bind的基础使用
- boost::bind的几种使用
- boost库 bind/function的使用
- bzoj 1015: [JSOI2008]星球大战starwar
- Android Zxing与Zbar比较
- Andriod ViewPager切换动画PageTransformer使用
- 如何设置Matlab的默认属性?
- 利用命令切换CTabCtrl控件的Tab页面
- 使用boost的bind
- JavaScript shift() 方法
- c++ 重载类型转换运算符
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
- JNI之helloworld
- 关于分布式WEB 的session共享(memcached+tomcat)
- 高德地图之语音导航
- 几句话总结常用的设计模式
- 自定义网络请求图片的大小