wheel: function&bind

#include <iostream>
#include <string>
#include <functional>  //function bind
#include <vector>
#include <algorithm>   //for_each
#include <iomanip>     //setw
#include <arpa/inet.h>

struct Points{
    double a,b;
    double multiply() { return a*b;}
};

struct Sum{
    int sum{0};
    void operator()(int n) { sum += n; }
    void setNum(int x)     { sum = x;  }
    void print()           { std::cout<<sum<<'\n'; }
};

bool isBigger(int x,int y)
{
    return x>y;
}

int check()
{
    union check{ int i; char ch; }c;
    printf("%p\n",&c.i);
    printf("%p\n",&c.ch);
    c.i =1;
    return (c.ch == 1);
}

int main()
{
    //std::function
    std::function<bool(int,int)> foo,lamda,bind;
    foo = isBigger;
    std::cout<<foo(10,9)<<'\n';

    lamda = [](int x,int y)->int{ return x>y;};
    std::cout<<lamda(10,9)<<'\n';

    foo = std::function<bool(int,int)>([](int x,int y){return x>y;});

    using namespace std::placeholders;
    bind = std::bind<bool>(isBigger,_2,_1);
    std::cout<<bind(10,9)<<'\n';

    //binding members
    Points pos = { 25.5,36.6 }; 
    
    auto BindStFn = std::bind(&Points::multiply,_1);
    std::cout<<BindStFn(pos)<<'\n';

    auto BindStData = std::bind(&Points::a,pos);
    std::cout<<BindStData()<<'\n';

    Points pos2 = { 60.1,33 };
    auto BindStData_1 = std::bind(&Points::a,_1);
    std::cout<<BindStData_1(pos2)<<'\n';    
    
    //std::for_each
    std::vector<int>nums{1,2,3,4,5};
    std::for_each(nums.cbegin(),nums.cend(),[](const int n){ std::cout<<std::setw(4)<<n;});
    
    std::cout<<'\n';
    std::for_each(nums.begin(),nums.end(),[](int& n){ n++;});

    Sum s = std::for_each(nums.cbegin(),nums.cend(),Sum());
    s.print();

    Sum* pSum = new Sum();
    auto bindSum = std::bind(&Sum::setNum,pSum,_1);
    bindSum(10);
    pSum->print();

    //----
    std::string PI = "3.1415926";
    
    unsigned char uc = std::stoi(PI,nullptr,10); 
    std::cout<<(int)uc<<'\n';

    
    //std::cout<<htons(0xffee);
    //char c1 = //htons(int_c);
    printf("int to uc:%d\r\n",uc);//换页键12 == c
    

    //---little or big
    int ret;
    ret = check();
    if(ret == 1) { printf("little\n"); }
     else         { printf("Big\n");    }
    
    //---
    int testInt = 1;
    unsigned char* pCC = (unsigned char*)&testInt;
    for(int i = 0; i < 4; i++)    printf("%p = %d\n",pCC+i,*(pCC+i));

    return 0;
}

2.

wheel: function&bind

 

wheel: function&bind

 

上一篇:PHP设计模式之建造者模式(Builder)代码实例大全(二)


下一篇:scrapy 框架的安装