0

I have a vector with an arbitrary number of doubles. For a class project, we are scanning for "pulses" or spikes in the values of these numbers and then need to compute the area of them by summing the numbers in a particular range.

I can do this using a simple for loop, however, our professor challenged us to use built in algorithms where possible. Reading over the various members in the algorithm class at cplusplus.com I am a little lost. Anybody able to please give me an example of how something like this could be done or point me to documentation that may be helpful in narrowing down my search?

Basically looking for a way to use one of the built-in algorithms to iterate through a vector, summing up adjacent values, and returning the total. I will pass the start and end position in the vector.

Lambda's are permitted, as well, though I am not very familiar with them. Seems like they could be useful for something like this, though.

CGutz
  • 537
  • 2
  • 7
  • 20
  • `our professor challenged us to use built in algorithms where possible. ` I can't believe it. A C++ teacher that actually wants you to use C++ :) – PaulMcKenzie Mar 27 '14 at 01:06

1 Answers1

1

Try std::accumulate to sum the values, std::for_each to loop.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thanks! Just found this after posting (of course I couldn't find it after searching several times before posting): http://stackoverflow.com/questions/3221812/sum-of-elements-in-a-stdvector #2 there lists an example of this that looks easy enough and is what I am going to try out. – CGutz Mar 27 '14 at 01:07