Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>template&lt;typename ForwardIterator, typename OutputIterator, typename UnaryPredicate&gt; void find_elements(ForwardIterator first, ForwardIterator last, OutputIterator out, UnaryPredicate pred) { while(first != last) { if(pred(*first)) *out++ = first; ++first; } } </code></pre> <p>Things to keep in mind:</p> <p>1.) You said that you want your container to be of <code>iterator</code> rather than <code>const_iterator</code>. The type will be the same as the begin and end range you pass to the function. For example, the type will be <code>const_iterator</code> for <code>const</code> containers, it will also be <code>const_iterator</code> if you use <code>vector::cbegin</code> and <code>vector::cend</code>, and it won't compile if you use differing iterators such as <code>vector::begin</code> and <code>vector::cend</code>.</p> <p>2.) Vectors often lose their iterator validity, so be careful with using these iterators. If you add to the vector, for example, every iterator returned by this function could be invalid. To prevent this, either use a different container (e.g. list) or use <code>vector::reserve</code>.</p> <p>3.) The forward iterator has to be something that supports ++, and when dereferenced, has the same type as InputIterator (e.g. <code>vector&lt;int&gt;::iterator</code>). It must also remain a valid iterator after incrementing it, or the function would be meaningless. The output iterator must go to a place with enough space to hold all the iterators found to satsify <code>pred</code>. If you do not know the space in advance, you can use <code>std::back_inserter</code> from <code>&lt;iterator&gt;</code> with a container that has <code>container::push_back</code> defined, and it will grow as needed.</p> <p>Here is a test of the function for you to understand how it works.</p> <pre><code>int main() { vector&lt;string&gt; ss = {"hi", "yog", "engils", "pog"}; // Define a predicate auto isSizeThree = [](string const &amp;s) { return s.size() == 3; }; // example one: Somehow I know how many satisfy the predicate. I just don't know where they are. vector&lt;vector&lt;string&gt;::iterator&gt; answer(2); find_elements(begin(ss), end(ss), begin(answer), isSizeThree); // Check answer cout &lt;&lt; "Test 1" &lt;&lt; endl; for(auto entry : answer) cout &lt;&lt; *entry &lt;&lt; endl; // Example two: I don't know how many there will be (more typical). If I use a continer with stuff in it - it will stack right on the back of it! find_elements(begin(ss), end(ss), back_inserter(answer), isSizeThree); // Check answer (has the answer from test 1 still in it) cout &lt;&lt; "Test 2" &lt;&lt; endl; for(auto entry : answer) cout &lt;&lt; *entry &lt;&lt; endl; // Example Three: Same as test 2 but clear the ansewr vector first. answer.clear(); find_elements(begin(ss), end(ss), back_inserter(answer), isSizeThree); // Check answer cout &lt;&lt; "Test 3" &lt;&lt; endl; for(auto entry : answer) cout &lt;&lt; *entry &lt;&lt; endl; } </code></pre>
 

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