Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest using boost range (algorithms &amp; adaptors) for ease of use, you'd write:</p> <pre><code>boost::copy( data | transformed(makeT) | filtered(validate) /* | indirected */, std::back_inserter(queue)); </code></pre> <p>Here is a complete working example of that:</p> <pre><code>#include &lt;boost/range.hpp&gt; #include &lt;boost/range/adaptors.hpp&gt; #include &lt;boost/range/algorithm.hpp&gt; #include &lt;boost/optional.hpp&gt; #include &lt;vector&gt; #include &lt;deque&gt; typedef boost::optional&lt;int&gt; T; typedef std::deque&lt;T&gt; Q; static T makeT(int i) { if (i%2) return T(); else return i; } static bool validate(const T&amp; optional) { return (bool) optional; // select the optional that had a value set } int main() { static const int data[] = { 1,2,3,4,5,6,7,8,9 }; Q q; using boost::adaptors::filtered; using boost::adaptors::transformed; // note how Boost Range elegantly supports an int[] as an input range boost::copy(data | transformed(makeT) | filtered(validate), std::back_inserter(q)); // demo output: 2, 4, 6, 8 printed for (Q::const_iterator it=q.begin(); it!=q.end(); ++it) { std::cout &lt;&lt; (*it? "set" : "unset") &lt;&lt; "\t" &lt;&lt; it-&gt;get_value_or(0) &lt;&lt; std::endl; } return 0; } </code></pre> <p><strong>Update</strong></p> <p>With a little help from this answer: <a href="https://stackoverflow.com/questions/6123327/use-boostoptional-together-with-boostadaptorsindirected">Use boost::optional together with boost::adaptors::indirected</a></p> <p>I now include an elegant demonstration of using the <code>indirected</code> range adaptor as well for immediate output of the queue (dereferencing the optionals):</p> <blockquote> <p><sub>Note that for (smart) pointer types there would <em>obviously</em> be no need to provide the <code>pointee&lt;&gt;</code> specialisation. I reckon this is by design: <em><a href="http://www.boost.org/doc/libs/1_47_0/libs/optional/doc/html/boost_optional/development.html#id555831" rel="nofollow noreferrer"><code>optional&lt;&gt; is not, and does not model, a pointer</code></a></em></sub></p> </blockquote> <pre><code>#include &lt;boost/range.hpp&gt; #include &lt;boost/range/adaptors.hpp&gt; #include &lt;boost/range/algorithm.hpp&gt; #include &lt;boost/optional.hpp&gt; namespace boost { template&lt;typename P&gt; struct pointee&lt;optional&lt;P&gt; &gt; { typedef typename optional&lt;P&gt;::value_type type; }; } typedef boost::optional&lt;int&gt; T; static T makeT(int i) { return i%2? T() : i; } static bool validate(const T&amp; optional) { return (bool) optional; } int main() { using namespace boost::adaptors; static int data[] = { 1,2,3,4,5,6,7,8,9 }; boost::copy(data | transformed(makeT) | filtered(validate) | indirected, std::ostream_iterator&lt;int&gt;(std::cout, ", ")); } </code></pre>
    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.
 

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