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++? G 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 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 Like