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

bookChallenge: Running Total with a Lambda

Tarea

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.

Solución

solution.cpp

solution.cpp

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain this in simpler terms?

What are the main points I should remember?

Can you give me an example?

close

Awesome!

Completion rate improved to 12.5

bookChallenge: Running Total with a Lambda

Desliza para mostrar el menú

Tarea

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.

Solución

solution.cpp

solution.cpp

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
single

single

some-alt