How to Capture Pairs in Lambda Expression in C++? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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++We can capture the variables from the surrounding scope of the lambda expression and then use them inside that function. The general syntax for a lambda expression is as follows: [capture-list] (parameters) -> return-type { // lambda body }To capture pairs, we mention the name of the pair in the square brackets. If we only use the name, then the pair will be captured by value. If we use the & operator with the name, the pair will be captured by reference. C++ Program to Capture Pairs in Lamda Expressions C++ // C++ program to demonstrate how to capture pairs in lambda // expression #include <iostream> #include <utility> using namespace std; int main() { // initialize some pairs pair<int, string> pair1 = make_pair(100, "geeks"); pair<int, string> pair2 = make_pair(200, "for"); pair<int, string> pair3 = make_pair(300, "geeks"); // capture pairs in lambda function auto lambda = [pair1, pair2, pair3]() { cout << pair1.first << " " << pair1.second << endl; cout << pair2.first << " " << pair2.second << endl; cout << pair3.first << " " << pair3.second << endl; }; // Call the lambda function to print the pairs lambda(); return 0; } Output100 geeks 200 for 300 geeks Time Complexity: O(1)Auxilary Space: O(1) Comment More infoAdvertise with us Next Article How to Capture Pairs in Lambda Expression in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ CPP-Functions cpp-pair CPP Examples +1 More Practice Tags : CPP Similar Reads How to Capture std::vector in Lambda Function? 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 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 Parse Mathematical Expressions in a C++ String? In C++, strings are sequences of characters stored in a char array. Strings are used to store words and text. We can also store mathematical expressions in this string. In this article, we will explore how to parse mathematical expressions in a C++ String. Example: Input:expression = (3 + 4) * 2 / ( 5 min read Lambda Expressions vs Function Pointers Function Pointer: A function pointer, or a subroutine pointer, or a procedure pointer, is a pointer that points to a function. In simple words, it is a pointer to the location inside the text section. It stores the address of a function and is used for passing a behavior as a parameter to another fu 7 min read Recursive lambda expressions in C++ A recursive lambda expression is the process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Ha 8 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 When to Prefer Lambda Expressions over Functors in C++? In C++, lambda expressions and functors (function objects) both are used for defining anonymous or named functions in C++. In this article, we will learn when to use a lambda expression instead of a functor in C++. When to Prefer Lambda instead of Functors?Prefer to use a lambda expression instead o 4 min read When to Use Lambda Expressions Instead of Functions in C++? In C++, both lambda expressions and functions are used to define operations that can be invoked somewhere else in the code. However, there are some cases where using lambda expressions can be more beneficial than using functions. In this article, we will learn when to use lambda expressions instead 4 min read How to Delete a Pair from a Multimap in C++? In C++, multimap stores key-value pairs and for the same key there can be multiple values in a multimap. In this article, we will learn how to delete a pair from a multimap in C++. Example Input:mpp={{"apple", 1},{"banana", 2},{"apple", 3}, {"orange", 4}}keyToRemove=applevalueToRemove=3Output:apple: 2 min read How to Create a Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps 2 min read Like