2-6 Working with Lambdas
在C++中使用匿名函数,格式如下:[] () {};
Using a Lambda to Print array Values
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <array> 3 #include <cstdint> 4 #include <iostream> 5 6 int main() 7 { 8 using MyArray = std::array<uint32_t, 5>; 9 MyArray myArray{ 1, 2, 3, 4, 5 }; 10 11 std::for_each(myArray.cbegin(), 12 myArray.cend(), 13 [](auto&& number) { 14 std::cout << number << std::endl; 15 }); 16 17 return 0; 18 }
Referencing a Closure in a Variable
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <array> 3 #include <cstdint> 4 #include <iostream> 5 #include <typeinfo> 6 7 int main() 8 { 9 using MyArray = std::array<uint32_t, 5>; 10 MyArray myArray{ 1, 2, 3, 4, 5 }; 11 12 auto myClosure = [](auto&& number) { 13 std::cout << number << std::endl; 14 }; 15 std::cout << typeid(myClosure).name() << std::endl; 16 17 std::for_each(myArray.begin(), 18 myArray.end(), 19 myClosure); 20 21 return 0; 22 }
下面是一些关于闭包的使用:
1.Passing a Closure into a Function
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <array> 3 #include <cstdint> 4 #include <functional> 5 #include <iostream> 6 #include <typeinfo> 7 8 using MyArray = std::array<uint32_t, 5>; 9 10 void PrintArray(const std::function<void(MyArray::value_type)>& myFunction) 11 { 12 MyArray myArray{ 1, 2, 3, 4, 5 }; 13 14 std::for_each(myArray.begin(), 15 myArray.end(), 16 myFunction); 17 } 18 19 int main() 20 { 21 auto myClosure = [](auto&& number) { 22 std::cout << number << std::endl; 23 }; 24 std::cout << typeid(myClosure).name() << std::endl; 25 PrintArray(myClosure); 26 return 0; 27 }
2.Copy an array into a vector through a lambda using the capture block
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <array> 3 #include <cstdint> 4 #include <functional> 5 #include <iostream> 6 #include <typeinfo> 7 #include <vector> 8 9 using MyArray = std::array<uint32_t, 5>; 10 using MyVector = std::vector<MyArray::value_type>; 11 12 void PrintArray(const std::function<void(MyArray::value_type)>& myFunction) 13 { 14 MyArray myArray{ 1, 2, 3, 4, 5 }; 15 16 std::for_each(myArray.begin(), 17 myArray.end(), 18 myFunction); 19 } 20 21 int main() 22 { 23 MyVector myCopy; 24 auto myClosure = [&myCopy](auto&& number) { 25 std::cout << number << std::endl; 26 myCopy.push_back(number); 27 }; 28 std::cout << typeid(myClosure).name() << std::endl; 29 30 PrintArray(myClosure); 31 32 std::cout << std::endl << "My Copy: " << std::endl; 33 std::for_each(myCopy.cbegin(), 34 myCopy.cend(), 35 [](auto&& number){ 36 std::cout << number << std::endl; 37 }); 38 39 return 0; 40 }
3.C++11 Compatible Lambda Function
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <array> 3 #include <cstdint> 4 #include <functional> 5 #include <iostream> 6 #include <typeinfo> 7 #include <vector> 8 9 using MyArray = std::array<uint32_t, 5>; 10 using MyVector = std::vector<MyArray::value_type>; 11 12 void PrintArray(const std::function<void(MyArray::value_type)>& myFunction) 13 { 14 MyArray myArray{ 1, 2, 3, 4, 5 }; 15 16 std::for_each(myArray.begin(), 17 myArray.end(), 18 myFunction); 19 } 20 21 int main() 22 { 23 MyVector myCopy; 24 auto myClosure = [&myCopy](const MyArray::value_type&number) { 25 std::cout << number << std::endl; 26 myCopy.push_back(number); 27 }; 28 std::cout << typeid(myClosure).name() << std::endl; 29 30 PrintArray(myClosure); 31 32 std::cout << std::endl << "My Copy: " << std::endl; 33 std::for_each(myCopy.cbegin(), 34 myCopy.cend(), 35 [](const MyVector::value_type&number){ 36 std::cout << number << std::endl; 37 }); 38 39 return 0; 40 }
4.keyward mutable