日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++11-std::functionbind

發布時間:2024/4/17 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++11-std::functionbind 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

std::function

可調用對象如下:

  • 是一個函數指針
  • 一個具有operator()成員函數的類對象
  • 一個可被轉換為函數指針的類對象
  • 是一個類成員指針
  • std::function是可調用對象包裝器,是一個類模版,可以容納除類成員函數指針外的所有可調用對象。

    頭文件是<functional>

    void func(void)

    {

    }

    class Foo

    {

    ? public:

    ? ? ? ? ?static int foo_func(int a);

    }

    class Bar

    {

    public:

    int operator()(int a);

    }

    std::function<void(void)> fr1=func;

    std::function<int(int)> fr2=Foo::foo_func;

    用途:

    1.可以作為回調函數,作為函數的參數

    call_when_even(int x, const std::function<void(int)>& f);

    ?

    std::bind

    std::bind可以把函數數的參數進行綁定,并把返回對象用std::function進行保存。

    1.將可調用對象與其參數以前綁定為一個仿函數

    2.將多元可調用對象轉成一元或者(n-1)元可調用對象

    auto fr=std::bind(output,std::placeholder::_1);

    auto fr=std::bind(output,std::placeholder::_1,2)(1);

    std::placeholder_1是一個占位符,代表這個位置將在函數調用的時候,被傳入的第一個參數所替代。

    1.使用binder簡化和增強的bind1st,bind2nd

    int count=std::count_if(coll.begin(),coll.end(),std::bind1st(less<int>(),10);

    std::bind2nd(less<int>(),10);

    2.使用組合bind函數

    查找集合中大于5,小于10的元素個數

    std::bind(std::greater<int>(),std::placeholders::_1,5);

    using std::placeholders::_1;

    auto f=std::bind(std::logical_and<bool>(),std::bind(std::greater<int>(),_1,5),std::bind(std::less_equal<int>(),_1,10);

    int count=std::count_if(coll.begin(),coll.end(),f);

    ?

    ?

    ?

    ?

    }

    總結

    以上是生活随笔為你收集整理的c++11-std::functionbind的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。