Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have basically two approaches:</p> <p>1) The thing you say in a comment above, write (iterators pointing at) the results into a container of iterators. That's going to look something like this:</p> <pre><code>template &lt;typename ForwardIterator, typename OutputIterator, typename UnaryPredicate&gt; void select_iterators(ForwardIterator first, ForwardIterator last, OutputIterator out, UnaryPredicate pred) { while (first != last) { if pred(*first) *out++ = first; ++first; } } </code></pre> <p>Then you call it like:</p> <pre><code>vector&lt;Foo&gt; myfoos; vector&lt;vector&lt;Foo&gt;::iterator&gt; results; select_iterators(myfoos.begin(), myfoos.end(), std::back_inserter(results), some_comparator); </code></pre> <p>You could actually define <code>select_iterators</code> in terms of other algorithms, using <code>copy_if</code> and <code>boost::counting_iterator</code>, but I don't think it's worth it when the direct implementation is so simple. It would look like:</p> <pre><code>template &lt;typename ForwardIterator, typename OutputIterator, typename UnaryPredicate&gt; void select_iterators(ForwardIterator first, ForwardIterator last, OutputIterator out, UnaryPredicate pred) { std::copy_if( boost::make_counting_iterator(first), boost::make_counting_iterator(last), out, [&amp;](ForwardIterator it) { return pred(*it); } ); } </code></pre> <p>2) Instead of testing all the values up front and writing the results somewhere, define an iterator that advances over the original range each time it is incremented until it finds the next match. Boost provides two ways of doing this, <code>boost::filter_iterator</code> and <code>boost::adaptors::filter</code>. So you could write:</p> <pre><code>auto results = boost::adaptors::filter(myfoos, some_comparator); </code></pre> <p>Then whatever you want to do with your results, you can iterate from <code>results.begin()</code> to <code>results.end()</code> as if it were a container. It's not a container, it doesn't satisfy the whole container interface. It satisfies an interface defined by Boost, named Range, which amounts to "can be iterated over". It's actually just a filtered view of <code>myfoos</code>, so no <code>Foo</code> object is copied or even moved.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload