最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11中std::function與std::bind的用法實例

 更新時間:2022年05月06日 10:39:02   作者:陽光檸檬_  
大家都知道C++11中增加了許多的新特性,下面這篇文章主要給大家介紹了關(guān)于C++11中std::function與std::bind的用法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

關(guān)于std::function 的用法:

其實就可以理解成函數(shù)指針

1. 保存自由函數(shù)

void printA(int a)
{
    cout<<a<<endl;
}

std::function<void(int a)> func;
func = printA;
func(2);

保存lambda表達(dá)式

std::function<void()> func_1 = [](){cout<<"hello world"<<endl;};
func_1();

保存成員函數(shù)

struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { cout << num_+i << '\n'; }
    int num_;
};

// 保存成員函數(shù)
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
Foo foo(2);
f_add_display(foo, 1);

在實際使用中都用 auto 關(guān)鍵字來代替std::function… 這一長串了。

關(guān)于std::bind 的用法:

看一系列的文字,不如看一段代碼理解的快

#include <iostream>
using namespace std;
class A
{
public:
    void fun_3(int k,int m)
    {
        cout<<k<<" "<<m<<endl;
    }
};

void fun(int x,int y,int z)
{
    cout<<x<<"  "<<y<<"  "<<z<<endl;
}

void fun_2(int &a,int &b)
{
    a++;
    b++;
    cout<<a<<"  "<<b<<endl;
}

int main(int argc, const char * argv[])
{
    auto f1 = std::bind(fun,1,2,3); //表示綁定函數(shù) fun 的第一,二,三個參數(shù)值為: 1 2 3
    f1(); //print:1  2  3

    auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3);
    //表示綁定函數(shù) fun 的第三個參數(shù)為 3,而fun 的第一,二個參數(shù)分別有調(diào)用 f2 的第一,二個參數(shù)指定
    f2(1,2);//print:1  2  3

    auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3);
    //表示綁定函數(shù) fun 的第三個參數(shù)為 3,而fun 的第一,二個參數(shù)分別有調(diào)用 f3 的第二,一個參數(shù)指定
    //注意: f2  和  f3 的區(qū)別。
    f3(1,2);//print:2  1  3


    int n = 2;
    int m = 3;

    auto f4 = std::bind(fun_2, n,placeholders::_1);
    f4(m); //print:3  4

    cout<<m<<endl;//print:4  說明:bind對于不事先綁定的參數(shù),通過std::placeholders傳遞的參數(shù)是通過引用傳遞的
    cout<<n<<endl;//print:2  說明:bind對于預(yù)先綁定的函數(shù)參數(shù)是通過值傳遞的


    A a;
    auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2);
    f5(10,20);//print:10 20

    std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);
    fc(10,20);//print:10 20

    return 0;
}

附:std::function與std::bind雙劍合璧

std::function可以指向類成員函數(shù)和函數(shù)簽名不一樣的函數(shù),其實,這兩種函數(shù)都是一樣的,因為類成員函數(shù)都有一個默認(rèn)的參數(shù),this,作為第一個參數(shù),這就導(dǎo)致了類成員函數(shù)不能直接賦值給std::function,這時候我們就需要std::bind了,簡言之,std::bind的作用就是轉(zhuǎn)換函數(shù)簽名,將缺少的參數(shù)補(bǔ)上,將多了的參數(shù)去掉,甚至還可以交換原來函數(shù)參數(shù)的位置,具體用法如下列代碼所示:

typedef std::function<void (int)> PrintFinFunction;
void print(const char *text, PrintFinFunction callback) {
    printf("%s\n", text);
    if (callback)
        callback(0);
}
// 類成員函數(shù)
class Test {
public:
    void printFinCallbackInter(int res) {
        cout << "Class Inter callback" << endl;
    }
};
// 函數(shù)簽名不一樣的函數(shù)
void printFinCallback2(int res1, int res2) {
    cout << "Different callback " << res1 << " " << res2 << endl;
}
Test testObj;
auto callback5 = std::bind(&Test::printFinCallbackInter, testObj, std::placeholders::_1);
print("test 5", callback5); //函數(shù)模板只有一個參數(shù),這里需要補(bǔ)充this參數(shù)
auto callback6 = std::bind(&printFinCallback2, std::placeholders::_1, 100);
print("test 6", callback6); //這里需要補(bǔ)充第二個參數(shù)

從上面的代碼中可以看到,std::bind的用法就是第一個參數(shù)是要被指向的函數(shù)的地址,為了區(qū)分,這里std::bind語句的左值函數(shù)為原函數(shù),右值函數(shù)為新函數(shù),那么std::bind方法從第二個參數(shù)起,都是新函數(shù)所需要的參數(shù),缺一不可,而我們可以使用std::placeholders::_1或std::placeholders::_2等等來使用原函數(shù)的參數(shù),_1就是原函數(shù)的第一個參數(shù),如此類推。

值得注意的有兩點:

一旦bind補(bǔ)充了缺失的參數(shù),那么以后每次調(diào)用這個function時,那些原本缺失的參數(shù)都是一樣的,舉個栗子,上面代碼中callback6,我們每次調(diào)用它的時候,第二個參數(shù)都只會是100。

正因為第一點,所以假如我們是在iOS程序中使用std::bind傳入一個缺失參數(shù),那么我們轉(zhuǎn)化后的那個function會持有那些缺失參數(shù),這里我們需要防止出現(xiàn)循環(huán)引用導(dǎo)致內(nèi)存泄漏。

總結(jié)

到此這篇關(guān)于C++11中std::function與std::bind用法的文章就介紹到這了,更多相關(guān)C++11 std::function與std::bind內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

简阳市| 宝丰县| 贺兰县| 温泉县| 贵港市| 日土县| 天峻县| 长治县| 永安市| 浮梁县| 普格县| 措勤县| 万州区| 元阳县| 彰化县| 凤庆县| 武清区| 拜泉县| 新宁县| 红安县| 穆棱市| 广河县| 法库县| 元谋县| 长垣县| 容城县| 岫岩| 南溪县| 三明市| 萨迦县| 怀化市| 永丰县| 新泰市| 太谷县| 二手房| 息烽县| 洮南市| 江北区| 黄浦区| 寻乌县| 那曲县|