title: efficient way to insert a unique_ptr into a map without deleting the pointer if the key already exists
body: <p>The easy way is obviosuly</p>
  
  <pre><code>std::map&lt;int,std::unique_ptr&lt;something&gt;&gt; mymap;
  
  auto f = mymap.find(5);
  std::unique_ptr&lt;something&gt; myptr;
  
  if (f == mymap.end())
      mymap.insert({5, std::move(myptr)});
  </code></pre>
  
  <p>However, this doesn't look too efficient, as I have to find the key in the map twice. One to check if the key doesn't exist, and the insert function will also do the same.</p>
  
  <p>If I simply use <code>mymap.insert({5, std::move(myptr)});</code> then my unique ptr (myptr) is gone if <code>pair.second</code> returns false (key already exists).</p>
  
  <p>EDIT:</p>
  
  <p>Apparently the answer is on C++17, with <code>try_emplace</code>, and it's already available in the compiler I'm using (vs2015) and since I'm working on a personal project, I can afford to use it.</p>