Home  >  Article  >  Backend Development  >  Application of C++ function default parameters and variable parameters in Lambda expressions

Application of C++ function default parameters and variable parameters in Lambda expressions

王林
王林Original
2024-04-22 15:06:02464browse

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++ 函数默认参数和可变参数在 Lambda 表达式中的应用

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn