stl bind2nd
来源:互联网 发布:skype聊天软件 编辑:程序博客网 时间:2024/06/12 19:32
比如我们有下面的类:
class ClxECS{public: int DoSomething() { // 这里以输出一句话来代替具体的操作 cout << "Output from method DoSomething!" << endl; return 0; };};
和下面的一个vector:
vector<ClxECS*> vECS;for (int i = 0; i < 13; i++){ ClxECS *pECS = new ClxECS; vECS.push_back(pECS);}
如果要对容器vECS中的所有对象都进行DoSomething()的操作,可以用下面的方法:
for_each(vECS.begin(), vECS.end(), mem_fun(&ClxECS::DoSomething));
(关于mem_fun的用法可以参考我的那篇《STL中mem_fun和mem_fun_ref的用法》)
当然,我们也可以用下面的方法:
int DoSomething(ClxECS *pECS){ return pECS->DoSomething();}for_each(vECS.begin(), vECS.end(), &DoSomething);
从上面的代码可以看到,两种方法其实都是调用类ClxECS的DoSomething()方法。在这里,方法DoSomething()是没有参数的,如果这个方法像下面那样是有参数的,该用什么方法传递参数呢?
class ClxECS{public: int DoSomething(int iValue) { cout << "Do something in class ClxECS!" << endl; cout << "The input value is: " << iValue << endl; return 0; }};
这个时候就该我们的bind2nd登场了!下面是具体的代码:
// mem_fun1是mem_fun支持一个参数的版本for_each(vECS.begin(), vECS.end(), bind2nd(mem_fun1(&ClxECS::DoSomething), 13));
或者:
int DoSomething(ClxECS *pECS, int iValue){ return pECS->DoSomething(iValue);}for_each(vECS.begin(), vECS.end(), bind2nd(ptr_fun(DoSomething), 13));
从上面的代码可以看出,bind2nd的作用就是绑定函数子的参数的。可是STL只提供了对一个参数的支持。如果函数的参数多于1个,那就无能为力了。
0 0
- stl bind2nd
- STL bind1st bind2nd详解
- STL bind1st bind2nd详解
- STL中bind2nd的用法
- STL中bind2nd的用法
- STL中bind2nd的用法
- stl 中bind1st和bind2nd
- bind2nd
- STL中bind1st和bind2nd的区别
- stl之bind1st,bind2nd解读(转)
- 【STL】bind1st与bind2nd函数解析
- STL binder1st binder2nd bind1st bind2nd区别
- STL bind1st、bind2nd的使用【转】
- STL中bind1st和bind2nd之解析
- STL: bind1st, bind2nd 的使用(C++)
- 【STL】bind1st与bind2nd函数解析
- STL约束器之bind2nd(x, y)实例解析
- STL的bind1st,bind2nd,mem_fun,mem_fun_ref使用示例
- python logging
- POJ 2459 Feed Accounting(水~)
- MongoDB分片实战(一):集群搭建 环境准备
- Log4j配置方式
- LintCode Binary Search 二分查找
- stl bind2nd
- 矩阵-DirectX与OpenGL的不同
- day16对于缓冲池以及自定义框架的理解
- Android Support ;v4、v7、v13的区别
- Unity3D学习(视频教程)
- Android学习系列(40)--Android主题和样式之系统篇
- MongoDB分片实战(二):Sharding 分片逻辑图
- Algebraic Teamwork
- Android_selector、shape