Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Running Total with a Lambda | Advanced Lambda Features
C++ Lambda Expressions

bookChallenge: Running Total with a Lambda

Aufgabe

Swipe to start coding

Update the addToTotal function so that the returned lambda returns the current total after adding the input value.

  • Implement the addToTotal function to return a lambda that captures total by reference.
  • The lambda should take an integer argument, add it to total, and return the updated total value.

In your main function:

  • Declare an integer variable named total and initialize it to zero.
  • Call addToTotal, passing total as an argument, and assign its return value to a variable named adder.
  • Call adder three times with the values 5, 10, and 15, each time printing the return value.
  • After the three calls, print the value of total to standard output.

Lösung

solution.cpp

solution.cpp

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

Awesome!

Completion rate improved to 12.5

bookChallenge: Running Total with a Lambda

Swipe um das Menü anzuzeigen

Aufgabe

Swipe to start coding

Update the addToTotal function so that the returned lambda returns the current total after adding the input value.

  • Implement the addToTotal function to return a lambda that captures total by reference.
  • The lambda should take an integer argument, add it to total, and return the updated total value.

In your main function:

  • Declare an integer variable named total and initialize it to zero.
  • Call addToTotal, passing total as an argument, and assign its return value to a variable named adder.
  • Call adder three times with the values 5, 10, and 15, each time printing the return value.
  • After the three calls, print the value of total to standard output.

Lösung

solution.cpp

solution.cpp

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
single

single

some-alt