title: Is it faster to use iterator-based search-and-use in STL associative containers?
body: <p>Let's say I'm trying to check if an item exists in an associative container, and do something to it if it does. The simple way to do this is as follows:</p>
  <pre><code>std::unordered_map&lt;std::string, Whatever&gt; thisIsAMap;
  
  // C++17
  if (thisIsAMap.count(&quot;key&quot;) &gt; 0u) { doThing(thisIsAMap[&quot;key&quot;]); }
  // C++20
  if (thisIsAMap.contains(&quot;key&quot;)) { doThing(thisIsAMap[&quot;key&quot;]); }
  </code></pre>
  <p>However, this has always seemed somewhat wasteful to me, as it involves finding the same item twice, which might be quite an expensive operation. Instead, I tend to do this:</p>
  <pre><code>auto found = thisIsAMap.find(&quot;key&quot;);
  if (found != thisIsAMap.end()) { doThing(found-&gt;second); }
  </code></pre>
  <p>This only requires finding the item once, and then using it.</p>
  <p>Is the latter approach actually any better? Are there any good reasons to use either approach over the other?</p>