C++超詳細梳理lambda和function的使用方法

lambda表達式

lambda表達式又稱為匿名表達式,是C11提出的新語法。[]存儲lambda表達式要捕獲的值,()內的參數為形參,可供外部調用傳值。lambda表達式可以直接調用

 // 1  匿名調用
    [](string name)
    {
        cout << "this is anonymous" << endl;
        cout << "hello " << name << endl;
    }("zack");

上述代碼定義瞭一個匿名函數後直接調用。我們可以通過auto初始化一個變量存儲lambda表達式

 // 2 通過auto賦值
    auto fname = [](string name)
    {
        cout << "this is auto  " << endl;
        cout << "hello " << name << endl;
    };
    fname("Rolin");

通過auto定義fname,然後存儲瞭lambda表達式,之後調用fname即可。也可以通過函數指針的方式接受lambda表達式

    typedef void (*P_NameFunc)(string name);
    // 3 函數指針
    P_NameFunc fname2 = [](string name)
    {
        cout << "this is P_NameFunc " << endl;
        cout << "hello " << name << endl;
    };
    fname2("Vivo");

P_NameFunc定義瞭fname2函數指針接受瞭lambda表達式。也可以通過function對象接受lambda表達式,function類是C11新增的語法。

// 4 function
    function<void(string)> funcName;
    funcName = [](string name)
    {
        cout << "this is function " << endl;
        cout << "hello " << name << endl;
    };
    funcName("Uncle Wang");

用一個function對象接受瞭lambda表達式,同樣可以調用該function對象funcName達到調用lambda的效果。

談談lambda的捕獲

1 值捕獲

    int age = 33;
    string name = "zack";
    int score = 100;
    string job = "softengineer";
    //值捕獲
    [age, name](string name_)
    {
        cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl;
    }("Novia");

上述lambda表達式捕獲瞭age和name,是以值的方式來捕獲的。所以無法在lambda表達式內部修改age和name的值,如果修改age和name,編譯器會報錯,提示無法修改const常量,因為age和name是以值的方式被捕獲的。

2 引用捕獲

    int age = 33;
    string name = "zack";
    int score = 100;
    string job = "softengineer";
    [&age, &name](string name_)
    {
        cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl;
        name = "Xiao Li";
        age = 18;
    }("Novia");

[]裡age和name前邊添加瞭&,此時age和name是以引用方式捕獲的。所以可以在lambda表達式中修改age和name的值。

C++的lambda表達式雖然可以捕獲局部變量的引用,達到類似閉包的效果,但不是真的閉包,golang和python等語言通過閉包捕獲局部變量後可以增加局部變量的聲明周期,C++無法做到這一點,所以下面的調用會出現崩潰。

vector<function<void(string)>> vec_Funcs;
void use_lambda2()
{
    int age = 33;
    string name = "zack";
    int score = 100;
    string job = "softengineer";
    vec_Funcs.push_back([age, name](string name_)
                        {   cout << "this is value catch " << endl;
                            cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl; });
    //危險,不要捕獲局部變量的引用
    vec_Funcs.push_back([&age, &name](string name_)
                        {   cout << "this is referenc catch" << endl;
                            cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl; });
}
void use_lambda3()
{
    for (auto f : vec_Funcs)
    {
        f("zack");
    }
}
int main(){
    use_lambda2();
    use_lambda3();
}

use_lambda2中將lambda表達式存儲在function類型的vector裡,當use_lambda2結束後,裡邊的局部變量都被釋放瞭,而vector中的lambda表達式還存儲著局部變量的引用,在調用use_lambda3時調用lambda表達式,此時訪問局部變量已經被釋放瞭,所以導致程序崩潰。

3 全部用值捕獲,name用引用捕獲

    int age = 33;
    string name = "zack";
    int score = 100;
    string job = "softengineer";
    [=, &name]()
    {
        cout << "age is " << age << " name is " << name << " score is " << score << " job is " << job << endl;
        name = "Cui Hua";
    }();

通過=表示所有變量都以值的方式捕獲,如果希望某個變量以引用方式捕獲則單獨在這個變量前加&。

4 全部用引用捕獲,隻有name用值捕獲

   int age = 33;
   string name = "zack";
   int score = 100;
   string job = "softengineer";
   [&, name]()
   {
        cout << "age is " << age << " name is " << name << " score is " << score << " job is " << job << endl;
   }();

通過&方式表示所有變量都已引用方式捕獲,如果希望某個變量以值方式捕獲則單獨在這個變量前加=。

萬能的function

我們可以用function存儲形參和返回值相同的一類函數指針,可調用對象,lambda表達式等。

void use_function()
{
    list<function<void(string)>> list_Funcs;
    //存儲函數對象
    list_Funcs.push_back(FuncObj());
    //存儲lambda表達式
    list_Funcs.push_back([](string str)
                         { cout << "this is lambda call " << str << endl; });
    //存儲全局函數
    list_Funcs.push_back(globalFun);
    for (const auto &f : list_Funcs)
    {
        f("hello zack");
    }
}

bind操作

C11同樣提供瞭bind操作,將原函數的幾個參數通過bind綁定傳值,返回一個新的可調用對象。

    //綁定全局函數
    auto newfun1 = bind(globalFun2, placeholders::_1, placeholders::_2, 98, "worker");
    //相當於調用globalFun2("Lily",22, 98,"worker");
    newfun1("Lily", 22);
    //多傳參數沒有用,相當於調用globalFun2("Lucy",28, 98,"worker");
    newfun1("Lucy", 28, 100, "doctor");
    auto newfun2 = bind(globalFun2, "zack", placeholders::_1, 100, placeholders::_2);
    //相當於調用globalFun2("zack",33,100,"engineer");
    newfun2(33, "engineer");
    auto newfun3 = bind(globalFun2, "zack", placeholders::_2, 100, placeholders::_1);
    newfun3("coder", 33);

placeholders表示占位符,_1表示新生成函數的第一個參數, _2表示新生成函數的第二個參數,將這些參數傳遞給原函數達到占位的效果,原函數的其餘參數通過bind綁定固定值。

接下來定義類

class BindTestClass
{
public:
    BindTestClass(int num_, string name_) : num(num_), name(name_) {}
    static void StaticFun(const string &str, int age);
    void MemberFun(const string &job, int score);
public:
    int num;
    string name;
};

實現靜態函數和成員函數

void BindTestClass::StaticFun(const string &str, int age)
{
    cout << "this is static function" << endl;
    cout << "name is " << str << endl;
    cout << "age is " << age << endl;
}
void BindTestClass::MemberFun(const string &job, int score)
{
    cout << "this is member function" << endl;
    cout << "name is " << name << endl;
    cout << "age is " << num << endl;
    cout << "job is " << job << endl;
    cout << "score is " << score << endl;
}

我們通過bind綁定靜態成員函數

    //綁定類的靜態成員函數,加不加&都可以
    // auto staticbind = bind(BindTestClass::StaticFun, placeholders::_1, 33);
    auto staticbind = bind(&BindTestClass::StaticFun, placeholders::_1, 33);
    staticbind("zack");

新生成的staticbind函數可以直接傳遞一個參數zack就完成瞭調用。接下來用bind綁定成員函數

    BindTestClass bindTestClass(33, "zack");
    // 綁定類的成員函數,一定要傳遞對象給bind的第二個參數,可以是類對象,也可以是類對象的指針
    // 如果要修改類成員,必須傳遞類對象的指針
    auto memberbind = bind(BindTestClass::MemberFun, &bindTestClass, placeholders::_1, placeholders::_2);
    memberbind("coder", 100);
    auto memberbind2 = bind(BindTestClass::MemberFun, placeholders::_3, placeholders::_1, placeholders::_2);
    memberbind2("coder", 100, &bindTestClass);
    //綁定類成員時,對象必須取地址
    auto numbind = bind(&BindTestClass::num, placeholders::_1);
    std::cout << numbind(bindTestClass) << endl;

當然也可以直接用function對象接受bind返回的結果

    // function接受bind返回的函數
    function<void(int, string)> funcbind = bind(globalFun2, "zack", placeholders::_1, 100, placeholders::_2);
    funcbind(33, "engineer");
    // function接受bind 成員函數
    function<void(string, int)> funcbind2 = bind(BindTestClass::MemberFun, &bindTestClass, placeholders::_1, placeholders::_2);
    funcbind2("docker", 100);
    function<void(string, int, BindTestClass *)> funcbind3 = bind(BindTestClass::MemberFun, placeholders::_3, placeholders::_1, placeholders::_2);
    funcbind3("driver", 100, &bindTestClass);
    // function 直接接受成員函數,function的模板列表裡第一個參數是類對象引用
    function<void(BindTestClass &, const string &, int)> functomem = BindTestClass::MemberFun;
    functomem(bindTestClass, "functomem", 88);
    // function 綁定類的靜態成員函數
    function<void(const string &)> funbindstatic = bind(&BindTestClass::StaticFun, placeholders::_1, 33);
    funbindstatic("Rolis");

lambda和bind的使用就介紹到這裡

源碼鏈接

視頻鏈接

到此這篇關於C++超詳細梳理lambda和function的使用方法的文章就介紹到這瞭,更多相關C++ lambda和function內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: