title: c++: Select a subset of a std::vector, based predefined element indices
body: <p>I am looking for an efficient way of either trimming or copying a subset of an existing std::vector. The criteria for elements to be eligible for the subset / remain is that their index is contained in a separate predefined std::vector.</p>
  
  <pre><code>e.g std::vector&lt;String&gt; Test = { "A", "B", "C", "D", "E"}
  
  std::vector&lt;int&gt; SelectionV = {1,2,5}
  
  Result = {"A", "B", "E"}
  </code></pre>
  
  <p>I will be doing this on a very large vector and probably on a regular basis, so am looking for as efficient method as possible.</p>
  
  <p>An alternative that I am also considering, but again unsure of an efficient method is...</p>
  
  <p>As the object Test is filled (in my case it is 3rd party defined object), it is the result of a single pass through using an iterator (no direct element access is possible). I was wondering if instead it is possible to only add to the Test vector elements that appear in the count defined in SelectionV</p>
  
  <p>e.g </p>
  
  <pre><code>int count = 0
  
  for (Iterator.begin, Iterator.end(), Iterator++) {
      if (count is a number contained in selectionV)
          add to Test
  }
  </code></pre>
  
  <p>but I assume that will result in a pass through the selectionV on each iteration, which would be far less efficient than simply adding all elements and later selecting a subset.</p>
  
  <p>Any help much appreciated.</p>