/* lambda.cpp */ #include#include #include using namespace std; // assign a value to each element of a vector void assign(int& v) { static int n = 1; v = n++; } // print out each element void print(int v) { cout << v << " "; } int main() { vector vec(10); // output initial value of each element for_each(vec.begin(), vec.end(), print); cout << endl; // assign a value to each element of a vector for_each(vec.begin(), vec.end(), assign); // output updated value of each element for_each(vec.begin(), vec.end(), print); return 0; }
Output:
$ g++ -std=c++11 -o lambda lambda.cpp $ ./lambda 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10Now, we can use lambda functions [](){} for the print() and assign() functions:
int main()
{
vector vec(10);
// output initial value of each element
// for_each(vec.begin(), vec.end(), print); ==>
for_each(vec.begin(), vec.end(), [](int v) {cout << v << " ";});
cout << endl;
// assign a value to each element of a vector
// for_each(vec.begin(), vec.end(), assign); ==>
for_each(vec.begin(), vec.end(), [](int& v) {static int n = 1; v = n++;});
// output updated value of each element
// for_each(vec.begin(), vec.end(), print); ==>
for_each(vec.begin(), vec.end(), [](int v) {cout << v << " ";});
return 0;
}
Here are more sample codes:
int main()
{
// (1)
std::cout << [](int a, int b){return a*b; }(4, 5) << std::endl; // 20
// (2)
auto f = [](int a, int b) { return a*b; };
std::cout << f(4, 5) << std::endl; // 20
}
The (1) and (2) are equivalent, and produced the results, 20.
Return from a lambda function
The compiler can deduce the return value type from a lambda function as shown in the case #1 of the example below. However, still we can explicitly specify its return type as in the case #2:
/* lam.cpp */ #includeusing namespace std; int main() { /* case #1 - compiler deduces return type */ cout << [](int n) {return n*n;} (5); cout << endl; /* case #2 - explicit return type */ cout << [](int n)->int {return n*n;} (5); return 0; }
Output:
$ g++ -std=c++11 -o lam lam.cpp $ ./lam 25 25
No comments:
Post a Comment