![]() |
In C++, lambda functions allow users to define an inline function anywhere in the program. They are also able to capture the objects from outside its definition. In this article, we will look at how to capture a std::vector object in lambda functions in C++. Capture std::vector in Lambda FunctionTo capture the std::vector container object that is present outside of the lambda function definition, we can use the capture clause of the lambda expression. The capture clause allows both capture by value and capture by reference method. Capture by Value [vector_name] (args){ // code } Capture by Reference [&vector_name] (args) { // code } C++ Program to Capture std::vector in Lambda FunctionC++
Output
Original Vector after calling addVectorV: 1 2 3 4 5 Original Vector after calling addVectorR: 2 3 4 5 6 |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |