Pass an Array into a Lambda Function in C++



In this article, we will learn how to pass an array into a lambda function in C++. A lambda function is a small, anonymous function that doesn't have a name and can access variables from the surrounding code.

Passing an array to a lambda function is a bit different from passing a single value because you need to define how the array will be handled inside the lambda. Let's see how to do that.

Passing an Array into a Lambda Function

We will cover four main methods to pass arrays into lambda functions in C++:

Passing an Array by Reference

In this approach, the lambda function captures the array by reference using the capture clause [&arr]. This means the lambda function can access and modify the original array. Any changes made to the array inside the lambda will reflect in the original array outside the lambda.

Example

Here's a complete C++ code where we pass the array by reference to a lambda function, allowing the lambda to access and modify the original array.

#include <iostream> int main() { // Approach 1: Pass array by reference in the capture clause int arr[] = {1, 2, 3, 4, 5}; auto lambda = [&arr]() { std::cout << "Array elements: "; for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } std::cout << "\n"; // Modify the array arr[0] = 10; }; lambda(); std::cout << "After modification, array elements: "; for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } return 0; } 

The output shows that the lambda function prints the original array, modifies the first element, and then the modified array is displayed.

Array elements: 1 2 3 4 5 After modification, array elements: 10 2 3 4 5 

Passing an Array by Parameter

In this approach, the array is passed as a parameter to the lambda function. The lambda takes a pointer to the array along with its size. This allows the lambda function to access and modify the elements of the array.

Example

Here, we pass the array arr to the lambda function by its pointer (int* array) along with the size (size_t size). Inside the lambda, the array is printed, and then the first element (arr[0]) is modified. The change is reflected in the original array.

#include <iostream> int main() { // Approach 2: Pass array as a parameter int arr[] = {1, 2, 3, 4, 5}; auto lambda = [](int* array, size_t size) { std::cout << "Array elements: "; for (size_t i = 0; i < size; ++i) { std::cout << array[i] << " "; } std::cout << "\n"; // Modify the array array[0] = 10; }; lambda(arr, 5); std::cout << "After modification, array elements: "; for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } return 0; } 

The output shows the lambda prints and modifies the array, with changes reflected outside the lambda.

Array elements: 1 2 3 4 5 After modification, array elements: 10 2 3 4 5 

Using std::array

In this approach, we use std::array, which is a safer and more convenient way to handle arrays in C++. We capture the array by reference in the lambda function and modify its contents.

Example

In this example, we define an array using std::array and capture it by reference in the lambda function. The lambda prints the array and modifies the first element. Since the array is captured by reference, the change is reflected in the original array.

#include <iostream> #include <array> int main() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; auto lambda = [&arr]() { std::cout << "Array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << "\n"; arr[0] = 11; // Modify the array }; lambda(); std::cout << "After modification, array elements: "; for (const auto& element : arr) { std::cout << element << " "; } return 0; } 

The output shows the lambda prints the array and modifies the first element, which is reflected in the original std::array.

Array elements: 1 2 3 4 5 After modification, array elements: 11 2 3 4 5 

Using std::vector

In this approach, we use std::vector, which is a dynamic array in C++ that provides more flexibility than raw arrays. The lambda function captures the std::vector by reference and can modify its contents.

Example

In this example, we use std::vector to define the vector vec. The lambda captures the vector by reference, prints the elements, and modifies the first element, which is reflected in the original vector.

#include <iostream> int main() { // Approach 1: Pass array by reference in the capture clause int arr[] = {1, 2, 3, 4, 5}; auto lambda = [&arr]() { std::cout << "Array elements: "; for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } std::cout << "\n"; // Modify the array arr[0] = 12; }; lambda(); std::cout << "After modification, array elements: "; for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } return 0; } 

The output shows the vector elements before and after the modification, reflecting the change made within the lambda function.

Array elements: 1 2 3 4 5 After modification, array elements: 12 2 3 4 5 

Conclusion

In this article, we learned four ways to pass arrays into lambda functions in C++. These methods include passing arrays by reference, by parameter, using std::array, and using std::vector. Each method helps us work with arrays inside the lambda function and modify them as needed.

Updated on: 2025-03-27T14:55:25+05:30

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements
close