C++ examples for STL:Lambda
Using the Lambda Capture Feature
#include <algorithm> #include <array> #include <cstdint> #include <functional> #include <iostream> #include <typeinfo> #include <vector> using MyArray = std::array<int, 5>; using MyVector = std::vector<MyArray::value_type>; void PrintArray(const std::function<void(MyArray::value_type)>& myFunction) { MyArray myArray{ 1, 2, 3, 4, 5 };//from w w w . j a v a 2 s . c om std::for_each(myArray.begin(), myArray.end(), myFunction); } int main() { MyVector myCopy; auto myClosure = [&myCopy](auto&& number) { std::cout << number << std::endl; myCopy.push_back(number); }; std::cout << typeid(myClosure).name() << std::endl; PrintArray(myClosure); std::cout << std::endl << "My Copy: " << std::endl; std::for_each(myCopy.cbegin(), myCopy.cend(), [](auto&& number){ std::cout << number << std::endl; }); return 0; }