• c++ for_each( )学习


    for_each()事实上是個 function template,其实质如下  [effective STL item 41]

    template<typename InputIterator, typename Function>
    Function for_each(InputIterator beg, InputIterator end, Function f) {
      while(beg != end) 
        f(*beg++);
    }
    能看懂吧!!!

    Object Oriented 与for_each 搭配

    1、不传入参数,使用function object

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<typeinfo>
    using namespace std;
    
    struct Play
    {
        void operator () (int i)
        {
            cout<<i<<endl;
        }
    };
    
    int main()
    {
        int a[] = { 1, 3, 4, 5};
        vector<int> vc(a, a+sizeof(a)/sizeof(int));
        for_each(vc.begin(), vc.end(), Play());
    }
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<typeinfo>
    using namespace std;
    
    struct Play
    {
        Play()
        {
            cout<<"new a Play"<<endl;
        }
        Play(const Play&)
        {
            cout<<"new a copy Play"<<endl;
        }
        void operator () (int i)
        {
            cout<<i<<endl;
        }
        ~Play()
        {
            cout<<"dispose a Play"<<endl;
        }
    };
    
    int main()
    {
        int a[] = { 1, 3, 4, 5};
        vector<int> vc(a, a+sizeof(a)/sizeof(int));
        for_each(vc.begin(), vc.end(), Play());
        cout<<"See something"<<endl;
    }
    结果如下:
    new a Play 




    new a copy Play 
    dispose a Play 
    dispose a Play 
    See something

    可以看到这个过程有两个Play对象生成,但是,用于输出元素的却是第一个对象(重载() 操作符),为什么? 
    这时候回去看for_each的源码,就会发现,它的返回值是function,以我的猜测,应该是这样的。

    Play() 生成一个临时的匿名的Play对象,传入for_each 函数里,然后执行完for_each 函数后,return一个function时,Play用复制构造函数生成一个Play对象,然后两个Play对象的生命周期都结束,于是依次销毁。

    2、传入参数

    可以通过构造函数的技巧传入参数

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<typeinfo>
    using namespace std;
    
    struct Play
    {
        const char* str;
        Play(const char* s):str(s) {}
        void operator () (int i)
        {
            cout<<str<<i<<endl;
        }
    };
    
    int main()
    {
        int a[] = { 1, 3, 4, 5};
        vector<int> vc(a, a+sizeof(a)/sizeof(int));
        for_each(vc.begin(), vc.end(), Play("Element:"));  //其实 还是关键看  Play函数如何实现的!
    }

    结果:
    Element:1                                                                                                                                                                          
    Element:3                                                                                                                                                                          
    Element:4                                                                                                                                                                          
    Element:5                                                                                                                                                                          

    Member function 与 for_each 搭配

    1、不传入参数

    通过mem_fun_ref() 这个funtion adapater 将 member funtion 转成 function object。

    先到这里吧 下次 继续    !!!!

  • 相关阅读:
    开源监控软件之争
    为什么很多公司都自主开发监控系统?
    为 UWP 应用提供的 .NET 网络 API
    亲,根据二八定律,你的监控工具可能白装了哦
    PHP7正式版测试,性能惊艳!
    Java Web 前端高性能优化(一)
    天下武功无坚不破,唯快不破!
    告警信息大爆炸,运维解放秘籍!
    第33节:Java面向对象中的异常
    第33节:Java面向对象中的异常
  • 原文地址:https://www.cnblogs.com/zhangkele/p/9373063.html
Copyright © 2020-2023  润新知