官术网_书友最值得收藏!

Returning a value from the Lambda expression

Our two preceding samples of the Lambda expression are just for the purpose to print on console. It means the function does not need to return any value. However, we can ask the Lambda expression to return a value for an instance if we do the calculation inside the function and return the calculation result. Let's take a look at the following code to examine the use of this Lambda:

    /* lambda_returning_value.cpp */
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

auto main() -> int
{
cout << "[lambda_returning_value.cpp]" << endl;

// Initializing a vector containing integer element
vector<int> vect;
for (int i = 0; i < 10; ++i)
vect.push_back(i);

// Displaying the elements of vect
cout << "Original Data:" << endl;
for_each(
begin(vect),
end(vect),
[](int n){
cout << n << " ";
});
cout << endl;

// Creating another vect2 vector
vector<int> vect2;
// Resize the size of vect2 exactly same with vect
vect2.resize(vect.size());
// Doubling the elements of vect and store to vect2
transform(
begin(vect),
end(vect),
begin(vect2),
[](int n) {
return n * n;
});

// Displaying the elements of vect2
cout << "Squared Data:" << endl;
for_each(
begin(vect2),
end(vect2),
[](int n) {
cout << n << " ";
});
cout << endl;

// Creating another vect3 vector
vector<double> vect3;
// Resize the size of vect3 exactly same with vect
vect3.resize(vect.size());
// Finding the average of the elements of vect
// and store to vect2
transform(
begin(vect2),
end(vect2),
begin(vect3),
[](int n) -> double {
return n / 2.0;
});

// Displaying the elements of vect3
cout << "Average Data:" << endl;
for_each(
begin(vect3),
end(vect3),
[](double d) {
cout << d << " ";
});
cout << endl;

return 0;
}

When we use the transform() method in the preceding code, we have a Lambda expression that returns a value from the calculation of n * n. However, there's no return type stated in the expression. This is because we can omit the statement of the return type since the compiler has understood that the expression will return an integer value. So, after we have another vector, vect2, which has the same size as vect, we can invoke the transform() method along with the Lambda expression, and the value of vect will be doubled and stored in vect2.

We can, if we want to, specify the return type to the Lambda expression. As we can see in the preceding code, we transformed the vect3 vector based on all values of the vect vector, but now we specify the return type to double using the arrow symbol (->). The result of the preceding code should be like the following screenshot:

As we can see from the preceding screenshot, we have successfully found the doubled and average result using the Lambda expression.

主站蜘蛛池模板: 盐津县| 汤阴县| 舟曲县| 西峡县| 星子县| 开鲁县| 塔城市| 望谟县| 田东县| 霍邱县| 五寨县| 轮台县| 普宁市| 平邑县| 黄山市| 堆龙德庆县| 富裕县| 安新县| 商都县| 宁海县| 奉节县| 河曲县| 静安区| 鹤山市| 广河县| 西乌| 临泉县| 合肥市| 化州市| 会同县| 兴宁市| 民乐县| 辽中县| 武陟县| 大丰市| 厦门市| 睢宁县| 满城县| 枣强县| 无极县| 塔城市|