• 《C++标准程序库》 第5章 Standard Template Library


    为了调用算法,必须含入头文件 <algorithm>

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<int> vect = {1,2,3,4};    // -std=c++0x
        std::cout<<*max_element(vect.begin(),vect.end())<<std::endl;
        std::cout<<*min_element(vect.begin(),vect.end())<<std::endl;
    }

    使用 Lambda 表达式的例子:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    struct User
    {
        int id;
    };
    
    int main()
    {
        std::vector<User> vect = {{2},{1},{3}};
        //以一元判断式作为算法的参数
        std::vector<User>::iterator it = find_if(vect.begin(),vect.end(),[&](User user) {
            return user.id == 3;
        });
        std::cout<<"it->id:"<<it->id<<std::endl;
        //以二元判断式作为算法的参数
        std::sort(vect.begin(),vect.end(),[&](User user1,User user2) {
            return user1.id < user2.id;
        });
        //以void函数作为算法的参数
        std::for_each(vect.begin(),vect.end(),[&](User user) {
            std::cout<<user.id<<std::endl;
        });
    }

    函数对象(仿函数)与std::bind1st() 、 std::bind2nd()

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    void print(int i,int j)
    {
        std::cout<<i<<"---"<<j<<std::endl;
    }
    
    //template <typename T>
    struct Print : public std::binary_function</*T*/int,/*T*/int,void>
    {
        void operator()(int i) const
        {   
            std::cout<<i<<std::endl;
        }   
        void operator()(int i,int j) const
        {   
            std::cout<<"i:"<<i<<"\tj:"<<j<<std::endl;
        }   
    };
    
    int main()
    {
        //std::ptr_fun是将函数指针转换为仿函数指针
        std::bind1st(std::ptr_fun(print),2)(1);
        std::bind2nd(std::ptr_fun(print),2)(1);
    
        std::vector<int> vect = {1,2,3};
        std::replace_if(vect.begin(),vect.end(),std::bind2nd(std::less<int>(),2),0);
        std::for_each(vect.begin(),vect.end(),Print());
        std::for_each(vect.begin(),vect.end(),std::bind1st(Print(),3));
        std::for_each(vect.begin(),vect.end(),std::bind2nd(Print(),3));
    }

    stl提供的less,greater 等内置仿函数可以直接使用 std::bind1st()、std::bind2nd() 函数绑定,如果要绑定自己定义的二元仿函数,那么就必须从binary_function派生出来。std::binary_function(T,T,void);  第三个参数表示 operator() 的返回值类型。以下附上 less<T>() 仿函数的源码:

      template<typename _Tp>
        struct less : public binary_function<_Tp, _Tp, bool>
        {   
          bool
          operator()(const _Tp& __x, const _Tp& __y) const
          { return __x < __y; }
        };  

    std::mem_fun_ref() 与 std::mem_fun() 的作用就是将一个"成员函数指针"包装成一个仿函数。mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref,当容器中存放的是对象的指针的时候用mem_fun。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    struct User
    {
        User(int _id):id(_id){}
        int id; 
        void print(int i)
        {   
            std::cout<<"i:"<<i<<std::endl;
        }   
    };
    
    int main()
    {
        std::vector<User> user_vect = {{1},{2},{3}};
        User *puser1 = new User(1);
        User *puser2 = new User(2);
        std::vector<User*> puser_vect = {puser1,puser2};
        std::for_each(user_vect.begin(),user_vect.end(),std::bind2nd(std::mem_fun_ref(&User::print),3));
        std::for_each(puser_vect.begin(),puser_vect.end(),std::bind2nd(std::mem_fun(&User::print),3));
    }
  • 相关阅读:
    Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition
    Java并发--Java线程面试题 Top 50
    Java并发--Timer和TimerTask
    Spring配置--Aop配置详情
    Spring配置--tx事务配置方式
    java并发--Callable、Future和FutureTask
    java并发--CountDownLatch、CyclicBarrier和Semaphore
    精通Matlab数字图像处理与识别
    油砂资源状况与储量评估方法
    家庭自制药蛋
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/3069545.html
Copyright © 2020-2023  润新知