This example illustrates how to use lambda expressions for concise function definitions.
cppCopy code#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::for_each(numbers.begin(), numbers.end(), [](int n) {
std::cout << n * n << " "; // Output: 1 4 9 16 25
});
std::cout << std::endl;
return 0;
}
Leave a Reply