Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C++ isn't meant to support laziness. Using lambdas instead of normal functions is no exception here.</p> <p>This</p> <pre><code>int f(vector&lt;int&gt; v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); copy(begin(v), end(v), ostream_iterator&lt;int&gt;(cout, " ")); cout &lt;&lt; endl; /* And then the function uses these helpers a few more times to justify making functions... */ } </code></pre> <p><strong>is much more succinct, standard, and readable</strong> than this</p> <pre><code>int f(vector&lt;int&gt; v) { auto make_unique = [](vector&lt;int&gt; &amp;v) { sort(begin(v), end(v)); auto unique_end = unique(begin(v), end(v)); v.erase(unique_end, end(v)); }; auto print_vector = [](vector&lt;int&gt; const &amp;v) { copy(begin(v), end(v), ostream_iterator&lt;int&gt;(cout, " ")); cout &lt;&lt; endl; }; make_unique (v); print_vector(v); /* And then the function uses these helpers a few more times to justify making functions... */ } </code></pre> <p>As a general rule of thumb, when your code itself isn't obscure (<code>erase</code> and <code>sort</code> and <code>copy</code> aren't obscure, they're standard C++ algorithms), adding more code only makes it worse.</p> <p>Furthermore: there is no reason to restrict the type of <code>v</code> to <code>vector&lt;int&gt;</code> in so many places.<br> Use type inference wherever the type doesn't matter.</p> <p>If you feel it should be a separate function, then make it a separate function entirely.<br> Don't use lambdas as a crutch for being lazy.</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. This table or related slice is empty.
    1. 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