Home >Backend Development >C++ >Application of C++ function default parameters and variable parameters in Lambda expressions
In Lambda expressions, default parameters allow you to specify default parameter values, while variable parameters allow you to pass an indefinite number of parameters. Default parameters should follow required parameters, while variadic parameters must be the last of the function parameters. These features simplify code and improve readability, such as adding prefixes and suffixes when working with lists of strings.
C Application of function default parameters and variable parameters in Lambda expression
Lambda expression is an anonymous function , which allows functions to be created at runtime. We can enhance the functionality of Lambda expressions using default parameters and variadic parameters.
Default parameters
Default parameters allow us to specify default values for the parameters of a Lambda expression. If no parameters are provided when called, default values will be used.
int add(int a, int b = 10) { return a + b; } int main() { auto sum = [](int a, int b) { return a + b; }; // 无默认参数 auto sumWithDefaultValue = [](int a, int b = 10) { return a + b; }; // 有默认参数 cout << sum(5, 6) << endl; // 输出: 11 cout << sumWithDefaultValue(5) << endl; // 输出: 15 return 0; }
Variable parameters
Variable parameters allow us to pass a variable number of parameters to a Lambda expression. Variable parameters must be the last parameter in the function parameters and are represented by ...
.
int sumAll(int n, ...) { va_list args; va_start(args, n); int sum = n; for (int i = 0; i < n; i++) { sum += va_arg(args, int); } va_end(args); return sum; } int main() { auto sumAllLambda = [](int n, ...) { va_list args; va_start(args, n); int sum = n; for (int i = 0; i < n; i++) { sum += va_arg(args, int); } va_end(args); return sum; }; cout << sumAll(3, 1, 2, 3) << endl; // 输出: 9 cout << sumAllLambda(4, 4, 5, 6, 7) << endl; // 输出: 22 return 0; }
Practical case
We can use default parameters and variable parameters to simplify the code and improve readability. For example, consider a function that processes a list of strings:
void processStrings(const vector<string>& strings, string defaultPrefix = "", string defaultSuffix = "") { for (const auto& str : strings) { cout << defaultPrefix << str << defaultSuffix << endl; } } int main() { vector<string> names = {"John", "Mary", "Bob"}; cout << "Prefix: \"Mr\", Suffix: \".\"" << endl; processStrings(names, "Mr", "."); // 使用默认参数 cout << "Prefix: empty, Suffix: \"!\"" << endl; processStrings(names, "", "!"); // 只使用可变参数 cout << "No prefix or suffix" << endl; processStrings(names); // 使用所有默认值 return 0; }
Output:
Prefix: "Mr", Suffix: "." Mr.John. Mr.Mary. Mr.Bob. Prefix: empty, Suffix: "!" John! Mary! Bob! No prefix or suffix John Mary Bob
The above is the detailed content of Application of C++ function default parameters and variable parameters in Lambda expressions. For more information, please follow other related articles on the PHP Chinese website!