Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Make your bool_xxx functions actually functors of a specific kind of type (tag dispatching would be enough). Then override || and &amp;&amp; for them such that these operators return a bool_and, or bool_or. Then you can use your bool_ predicates like so:</p> <pre><code> std::find_if(vect.begin(), vect.end(), bool_x1() || bool_x2() && (bool_x3() || bool_x4() && bool_x5())); </code></pre> <p>If you're tempted to write a "where" function then you apparently want to do this more than once with a different set of bool_xxx functions. Even if you know that you want a certain type of composition now, you may as well make it as universal as possible. This is how I'd do it.</p> <p>Edit:</p> <p>Based on this comment: <i>@Jerry: For example I need to know: id=where(v &lt; 10.0 &amp;&amp; v>1.0); and somewhere later I would like to know: id=where(v &lt; fun(v));</i> you may be better off with boost::lambda:</p> <pre><code> namespace l = boost::lambda; std::find_if(vect.begin(), vect.end(), l::_1 &lt; 10.0 && l::_1 > 1.0); std::find_if(vect.begin(), vect.end(), l::_1 &lt; l::bind(fun, l::_1)); </code></pre> <p>Or, if you hate lambda or aren't allowed to use it...or just want a very slightly nicer syntax (but inability to use functions directly) then just make your own placeholder type and override it to return bool_xxx functors on operators &lt;, >, etc...</p> <p>Edit2: Here's an untested where that returns a vector of iterators to all objects matching:</p> <pre><code> template &lt; typename ForwardIter, typename Predicate > std::vector&lt;ForwardIter> where(ForwardIter beg, ForwardIter end, Predicate pred) { ForwardIter fit = std::find_if(beg,end,pred); if (fit == end) return std::vector&lt;ForwardIter>(); ForwardIter nit = fit; ++nit; std::vector&lt;ForwardIter> collection = where(nit,end,pred); collection.push_front(fit); return collection; } </code></pre> <p>It's recursive and could be slow on some implementations but there's one way to do it.</p>
 

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