How To Capture Stdvector In Lambda Function In C++ - C++ Programming Tutorial
C++ Course / Advanced Topics / How To Capture Stdvector In Lambda Function In C++

How To Capture Stdvector In Lambda Function In C++

BLUF: Mastering How To Capture Stdvector In Lambda Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Capture Stdvector In Lambda Function In C++

C++ is renowned for its efficiency. Learn how How To Capture Stdvector In Lambda Function In C++ enables low-level control and high-performance computing in the tutorial below.

Programmers have the flexibility to declare an inline function within the code using lambda functions in C++. Lambda functions have the ability to capture external objects within their scope. In this article, we will explore the process of utilizing C++ lambda functions to capture a std::vector object.

Capture std::vector in Lambda Function.

The std::vector container object located external to the lambda function declaration can be captured through the lambda expression's capture clause. The capture clause permits both capturing by value and by reference methods.

Capture by Value:

Example

[vector_name] (args){
    // code
}

Capture by Reference:

Example

[&vector_name] (args) {
    // code
}

Program:

Let's consider an example to demonstrate how to capture a std::vector within a lambda function in the C++ programming language.

Example

#include <algorithm> 
#include <iostream> 
#include <vector> 
using namespace std; 
int main() 
{ 
vector<int> vec = { 7, 9, 8, 1, 1 }; 
auto printVector = [&vec]() mutable { 
for (auto elem : vec) { 
cout << elem << " "; 
} 
cout << endl; 
}; 
auto addVectorV = [vec]() mutable{ 
for (auto& elem : vec) { 
elem = elem + 1; 
} 
}; 
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; 
}

Output:

Benefits of std::vector in Lambda Function in C++

In C++, using std::vector with lambda functions can improve the code's expressiveness and flexibility in several ways. Here are a few specific advantages:

  • Dynamic Resizing: As std::vector offers dynamic resizing, managing collections of objects is made simple, and you don't have to worry about their fixed size. When combined with std::vector, lambda functions can perform dynamic operations like filtering, mapping, and element transformation.
  • Brief Syntax: Code can be written more succinctly and expressively when lambda functions and std::vector are combined. Lambdas can write operations like iteration, transformation, and filtering directly, negating the need for explicit loops and temporary variables.
  • Readability and Maintainability: By keeping the logic close to where it's utilized, lambda functions that are specified inline can make the code easier to read. Since it identifies the container being worked on, the code is easier to comprehend when lambda operations' results are stored in std::vector.
  • Algorithm Compatibility: The C++ Standard Template Library (STL) contains several readily compatible algorithms with std::vector . Lambda functions can apply algorithms such as std::transform, std::foreach, and std::removeif to vectors, offering a strong and adaptable method of processing items.
  • Automatic Memory Management: Memory management is handled automatically by std::vector , ensuring that elements are allocated and deallocated effectively.

By defining operations through lambda expressions, manual memory handling becomes unnecessary, reducing the chances of memory leaks and errors.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience