How to Capture std::vector in Lambda Function? Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 Function C++ // C++ program to capture std::vector in a lambda function #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = { 1, 2, 3, 4, 5 }; // Lambda function capturing vector by reference auto printVector = [&vec]() mutable { for (auto elem : vec) { cout << elem << " "; } cout << endl; }; // modifying vector by capturing using value auto addVectorV = [vec]() mutable{ for (auto& elem : vec) { elem = elem + 1; } }; // modifying vector by capturing using reference auto addVectorR = [&vec]() { for (auto& elem : vec) { elem = elem + 1; } }; addVectorV(); cout << "Original Vector after calling addVectorV: "; printVector(); addVectorR(); cout << "Original Vector after calling addVectorR: "; printVector(); return 0; } OutputOriginal Vector after calling addVectorV: 1 2 3 4 5 Original Vector after calling addVectorR: 2 3 4 5 6 Comment More infoAdvertise with us Next Article How to Capture std::vector in Lambda Function? A ashish_rao_2373 Follow Improve Article Tags : C++ Programs C++ cpp-vector CPP Examples Practice Tags : CPP Similar Reads How to Return a Vector From a Function in C++? In C++, by returning vectors, users to return multiple values at once from a function as the elements of vector. In this article, we will learn how to return a vector from a function in C++.The recommended way to return a vector from a function is by using return vector by value method. Letâs take a 3 min read How to Find the Mode of Vector in C++? In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector = 2 min read How to Capture Pairs in Lambda Expression in C++? In C++, lambda expressions provide an easy way to define anonymous functions inline. They also allow these inline functions to capture some variables from the surrounding scope. In this article, we will learn how to capture pairs in lambda functions in C++ Capture Pairs in Lambda Expressions in C++W 2 min read How to Pass an Array into a Lambda Function in C++? In C++, a lambda function, also known as a lambda expression, is a way of defining an anonymous function object right at the location where it is invoked or passed as an argument to a function. In this article, we will learn how to pass an array into a lambda function in C++. Passing an Array into a 2 min read How to use C++ Lambdas with Capture Clauses? In C++, lambda expressions provide a convenient way to create one-liner functions. One of the key features of lambdas is their ability to capture variables from their surrounding scope by using capture clauses. This allows lambdas to access and modify variables from the outer scope, even after the l 3 min read Like