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

Asked 1 weeks ago, Updated 1 weeks ago, 1 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


© 2022 pinfo. All rights reserved.