title: c++ computing a new vector which has deltas from an existing vector
body: <p>I'm working on learning C++ STL algorithms. I need help trying to find a function to create a vector of deltas from values in an existing vector. In other words:</p>
  
  <ul>
  <li>delta0 = abs(original1 - original0) </li>
  <li>delta1 = abs(original2 - original1) </li>
  <li>and so on.</li>
  </ul>
  
  <p>I'm looking for something concise, like R's "diff" function mentioned here:
  <a href="https://stackoverflow.com/questions/30542872/computing-a-new-vector-which-has-deltas-from-an-existing-vector"> computing a new vector which has deltas from an existing vector</a></p>
  
  <p>I found the transform function but it seemed to operate on a single element at a time. It didn't seem to allow parameters of iterator in the function supplied to transform so I was limited to the current element only. I'm trying to learn STL algorithms so I don't really need any libraries that may have "diff" implemented. I would just like to see a way to use STL functions to solve this if there is a concise way I'm not aware of.</p>
  
  <p>Here is an example with the section in question commented:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  int main() {
      vector&lt;int&gt; v = { 1, 2, 3, 4, 5 };
      vector&lt;int&gt; delta;
  
      //---------------------------------------
      // way to do this with STL algorithms?
      for (auto i = v.begin()+1; i != v.end(); i++) {
          delta.push_back(abs(*i - *(i - 1)));
      }
      //---------------------------------------
      for (int i : delta) {
          cout &lt;&lt; i &lt;&lt; " ";
      }
      return 0;
  }
  </code></pre>