title: Replacing a function with a lambda in C++
body: <p>I have a function that just sets a value of 1 to a struct member variable <code>length</code>. In modern C++ this doesn't seem like good code style. Could this be done with a lambda?  </p>
  
  <pre><code>void setEdgeLengths(Koala::AssocArray &lt;koalaGraph::PEdge, Koala::DijkstraHeap::EdgeLabs&lt;int &gt;&gt; &amp;edgeMap, std::vector&lt;koalaGraph::PEdge&gt;&amp; E) 
  {
      for (size_t i = 0; i &lt; E.size(); i++) {
      edgeMap[E[i]].length = 1;
      }
  }
  </code></pre>
  
  <p>The reason I am asking is <a href="https://shaharmike.com/cpp/lambdas-and-functions/" rel="nofollow noreferrer">https://shaharmike.com/cpp/lambdas-and-functions/</a> suggests that the lambda will be faster than an ordinary function.</p>
  
  <blockquote>
    <p>Lambdas are also awesome when it comes to performance. Because they are objects rather than pointers they can be inlined very easily by the compiler, much like functors. This means that calling a lambda many times (such as with std::sort or std::copy_if) is much better than using a global function. This is one example of where C++ is actually faster than C.</p>
  </blockquote>