August 18, 2005

bind2st for_each 和 transform

作者: winter

今天STL论坛上有个朋友问关于bind2st使用的问题,开始以为很简单:

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;
    }


    看来不是那么容易,STL在使用之初,确实不那么容易,其编程思想很不错,但是如果没有习惯这种风格,非常容易出现问题,加上编译错误提示信息不友好,也很难改正错误。因此,建议初学者尽量不要去使用一些麻烦的操作。多使用容器自带的函数,一步一步来。

    | 引用