I'd like to ask you a question about C++ accumulate.

Asked 2 years ago, Updated 2 years ago, 25 views

Hello!

inputArray: [5, 1, 2, 3, 0, 1, 5, 0, 2] I want to create a code that adds numbers sequentially in that array and returns a result when I check for zero.

If possible, I tried to solve it using the accumulate algorithm, but is there a way to check 0 and escape?

autolambda = [&](inta, intb) { return a + b; /* How to escape? */ }; 
return accumulate(inputArray.begin(), inputArray.end(), 0, lambda);

c++

2022-09-20 08:39

1 Answers

There was a simpler solution than I thought.

return accumulate(inputArray.begin(), find(inputArray.begin(), inputArray.end(), 0), 0); 


2022-09-20 08:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.