C++-----lambda使用


lambda是匿名函數,可以拿來當作inline函數使用(用於解決程序員的“起名困難綜合症”)

lambda函數形式:

  [...] (...) ... {...}

[] 內是一個capture,可以在lambda內部訪問的"nonstatic外部變量",如果沒有要訪問的變量,可以為空。static變量是可以直接被訪問的。

() 內是參數,和函數參數一樣。

... 是mutable, 異常明細, 屬性說明符(noexcept等), 或者返回類型。如果其中之一出現,那么必須出現()。

{} 內是函數體,在這里面寫明lambda要完成的工作。

例如:

#include <iostream>

using namespace std;

int main()
{
    []() { cout << "hello, world" << endl; }();
    return 0;
}

最后的一個()是函數調用。

也可以以如下方式調用:

auto f = []() { cout << "hello, world" << endl; };
f();

使用參數:

#include <iostream>

using namespace std;

int main()
{
    auto f = [](int a) { cout << "hello, world " << a << endl; };
    f(12);
    return 0;
}

返回類型:

#include <iostream>

using namespace std;

int main()
{
    auto f = [](int a) { cout << "hello, world " << a << endl; return a; };
    auto k = f(12);
    cout << k << endl;
    return 0;
}

C++的類型推導系統會自動推導出函數的返回類型,也可以寫明:

// return int
auto f = [](int a) -> int { cout << "hello, world " << a << endl; return a; }; 
// return double
auto f = [](int a) -> double { cout << "hello, world " << a << endl; return a; };

Capture -- passing by value

#include <iostream>

using namespace std;

int main()
{
    int xxx = 10;
    auto f = [xxx]() { cout << "hello, world " << xxx << endl;  };
    f();
    return 0;
}

將外部變量passing by value給lambda,lambda可以在內部訪問xxx,但是不可以修改xxx。

Capture -- passing by reference

#include <iostream>

using namespace std;

int main()
{
    int xxx = 10;
    auto f = [&xxx]() { cout << "hello, world " << ++xxx << endl;  };
    cout << xxx << endl;
    f();
    cout << xxx << endl;
    return 0;
}

lambda可以在內部修改xxx,並且會影響到外部變量。

特殊的形式:

    [=] (...) ... {...}    將外部所有變量傳遞給lambda,但是不能在內部修改 -- passing by value

    [&] (...) ... {...}     將外部所有變量傳遞給lambda, 可以在內部修改,並且影響外部變量 -- passing by ref..

    [=,&y] (...) ... {...}   將所有變量傳遞給lambda,y可以在內部修改,其余變量不行

    [y, &] (...) ... {...}   將所有變量傳遞給lambda,y不可以在內部修改,其余變量可以

mutable:

// from The C++ Standard Library

#include <iostream>

using namespace std;

int main()
{
    int id = 0;
    auto f = [id]() mutable { cout << "id: " << id << endl; ++id; };
    id = 42;
    f();
    f();
    f();
    cout << id << endl;
    return 0;
}
// 輸出如下:
// id: 0
// id: 1
// id: 2
// 42

  在這段代碼里由於有了mutable關鍵字,使得lambda變為了passing by value與passing by reference的混合體,在第一次調用時會制造一個臨時的副本,之后的調用都會去修改這一個副本。值得注意的是:lambda從定義時捕獲能夠訪問的值,此時id = 0,與f何時被調用無關。

不建議寫出lambda的類型,如果非要寫出類型,可以使用decltype(),或者使用STL內的function<>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM