当前位置: 代码迷 >> 综合 >> STL之bind2st for_each 和 transform
  详细解决方案

STL之bind2st for_each 和 transform

热度:67   发布时间:2024-01-14 07:26:24.0

由一个问题开始吧:

void print(int& a,const int b) { a+=b; } int main() { list<int> my_list; ......... for_each(my_list.begin(),my_list.end(), bind2nd(print,3) ); } 目的是依次循环,每个节点加3 想通过bind2nd使函数的第二个值绑定为3 可是通过不了,这是错在哪 如果要达到目的的话,应该怎么改呢??

后来一调试,发现不是那么容易。你能发现问题在哪儿吗?

后来看了帮助文档才解决,看看我的答复:

  • for_each 是需要使用functor , bind2st 的对象也是functor 而不是函数。
  • 如果需要使用bind2st,那么你需要从binary_function继承.
  • for_each不会让你修改元素, 因此你的要求是达不到的。STL编程手册中就有说明: for_each的 “UnaryFunction does not apply any non-constant operation through its argument”
  • 如果要达到要求,你可以使用transform. 以下代码就是一个例子: struct printx: public binary_function<int, int, int> {   int operator()(int a, int b)const   {      cout<<a+b<<endl;     return a+b;   } }; int main() {     vector<int> my;     my.push_back(0);     my.push_back(1);     my.push_back(2);     copy(my.begin(), my.end(), ostream_iterator<int>(cout, " "));     cout<<"/n-----"<<endl;     for_each(my.begin(),my.end(), bind2nd(printx() , 3) );     cout<<"/n-----"<<endl;     copy(my.begin(), my.end(), ostream_iterator<int>(cout, " "));     cout<<"/n-----"<<endl;     transform(my.begin(),my.end(),my.begin(), bind2nd(printx() , 3) );     cout<<"/n-----"<<endl;     copy(my.begin(), my.end(), ostream_iterator<int>(cout, " "));     return 0; }