- Learning C++ Functional Programming
- Wisnu Anggoro
- 220字
- 2021-07-02 20:51:38
Preparing the value using initialization captures
Another great feature of the Lambda expression coming up in C++14 is its initialization captures. The expression can capture a value of the variable and assign it to the expression's variable. Let's take a look at the following piece of code implementing the initialization captures:
/* lambda_initialization_captures.cpp */
#include <iostream>
using namespace std;
auto main() -> int
{
cout << "[lambda_initialization_captures.cpp]" << endl;
// Initializing a variable
int a = 5;
cout << "Initial a = " << a << endl;
// Initializing value to lambda using the variable
auto myLambda = [&x = a]() { x += 2; };
// Executing the Lambda
myLambda();
// Displaying a new value of the variable
cout << "New a = " << a << endl;
return 0;
}
As we can see in the preceding code, we have an int variable named a with the value 5. The Lambda expression, myLambda, then captures the a value and executes it in the code. The result is that now the a value will be 7 since it is added by 2. The following output screenshot should appear in our console window when we run the preceding code:

From the preceding snapshot, we see that we can prepare the value to be included in the calculation inside the Lambda expression.
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- Spring 5企業(yè)級(jí)開(kāi)發(fā)實(shí)戰(zhàn)
- 數(shù)據(jù)結(jié)構(gòu)和算法基礎(chǔ)(Java語(yǔ)言實(shí)現(xiàn))
- 圖解Java數(shù)據(jù)結(jié)構(gòu)與算法(微課視頻版)
- PHP+MySQL+Dreamweaver動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)實(shí)例教程
- Python編程與幾何圖形
- Python數(shù)據(jù)分析從0到1
- Apache Kafka Quick Start Guide
- ScratchJr趣味編程動(dòng)手玩:讓孩子用編程講故事
- Xcode 6 Essentials
- HTML5移動(dòng)前端開(kāi)發(fā)基礎(chǔ)與實(shí)戰(zhàn)(微課版)
- 啊哈C語(yǔ)言!:邏輯的挑戰(zhàn)(修訂版)
- Developing Java Applications with Spring and Spring Boot
- HTML5+CSS3+jQuery Mobile+Bootstrap開(kāi)發(fā)APP從入門(mén)到精通(視頻教學(xué)版)
- R語(yǔ)言數(shù)據(jù)分析從入門(mén)到實(shí)戰(zhàn)