I'm new to lambda expression and little confused why I'm getting the error here?
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = { 11, 21, 4, 13 };
for_each(arr, arr + 4, [arr](int x) {
cout << x;
});
return 0;
}
I just add LAMBDA for this function.
void fun1(int x)
{
cout << x << " ";
}
Here is the error message on visual studio.
'main::<lambda_4ee0815d3a456ed46cc70a2a94c10f76>::arr':
array initialization requires a brace-enclosed initializer list Project1
You cannot copy arrays, so you can capture arr
by reference instead if you actually need it:
for_each(arr, arr + 4, [&arr](int x) { cout << x; });
// ^^^
However, since you don't refer to the array in the lambda body, you don't need to capture it at all:
for_each(arr, arr + 4, [](int x) { cout << x; });
// ^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.