Lambda Expressions

This example illustrates how to use lambda expressions for concise function definitions.

cppCopy code#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector&lt;int&gt; numbers = {1, 2, 3, 4, 5};
std::for_each(numbers.begin(), numbers.end(), &#91;](int n) {
    std::cout &lt;&lt; n * n &lt;&lt; " "; // Output: 1 4 9 16 25
});
std::cout &lt;&lt; std::endl;
return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *