title: How to obtain index of element from predicate passed to some STL algorithm?
body: <p>Say, I have vector of elements and a mask array, and I want to extract elements from vector with true corresponding mask value to separate vector. Is there a way to use <code>std::copy_if</code> for this purpose? The problem is, I only have <em>value</em> of element inside predicate, not <em>iterator</em> to it, so I cannot know the actual index to address mask array.</p>
  
  <p>I can directly manipulate addresses like this:</p>
  
  <pre><code>vector&lt;bool&gt; mask;
  vector&lt;int&gt; a, b;
  copy_if(a.begin(), a.end(), b.begin(), [&amp;] (int x) -&gt; bool { 
    size_t index = &amp;x - &amp;a[0]; // Ugly...
    return mask[index];
  });
  </code></pre>
  
  <p>However, I find this to be ugly solution. Any better ideas?</p>
  
  <p><strong>Update</strong>: Another possible solution is to use external iterator on mask:</p>
  
  <pre><code>vector&lt;bool&gt; mask;
  vector&lt;int&gt; a, b;
  auto pMask = mask.begin();
  copy_if(a.begin(), a.end(), b.begin(), [&amp;] (int x) { 
    return *pMask++;
  });
  </code></pre>
  
  <p>However, this solution requires additional variable in outer namespace which still is not desirable.</p>