title: Deleting pointer to object in unordered_map
body: <p>I am trying to delete references to <code>unordered_map</code> element. The part of my classes responsible for that looks like this:</p>
  
  <pre><code>class Edge:
  {
      public:
          Node* from;
          Node* to;
  }
  
  class Node:
  {
      public:
          std::string name;
          bool to_delete;
  }
  
  class Graph:
  {
      public:
          std::unordered_map&lt;std::string, Node&gt; nodes;
          std::vector&lt;Edge&gt; edges;
  }
  </code></pre>
  
  <p>and in my main file code I am doing something like this:</p>
  
  <pre><code>    Node n("My node");
      graph.nodes.insert({n.name,n});
      edge.from = &amp;graph.nodes[n.name];
  
      // Some other stuff
  
      for(auto&amp; edge : graph.edges)
      {
          if(edge.from-&gt;to_delete)
          {
              graph-&gt;nodes-&gt;erase(edge.from-&gt;name);
              delete edge.from;
              edge.from = NULL;
          }
          if(edge.to-&gt;to_delete)
          {
              graph-&gt;nodes-&gt;erase(edge.to-&gt;name);
              delete edge.to;
              edge.to = NULL;
          }
  
          if(edge-&gt;from &amp;&amp; edge-&gt;to)
              DoSomethingWithNodes(edge-&gt;from, edge-&gt;to);
          else
              removeEdge(edge);
      }
  </code></pre>
  
  <p>Currently I am deleting pointers based on this <a href="https://stackoverflow.com/a/13223431/1080517">answer</a> but I am getting segmentation error on line with <code>delete</code>. In this same answer there is also suggestion to use <code>smart pointers</code>. I am not sure about using here <a href="http://en.cppreference.com/w/cpp/memory/shared_ptr" rel="nofollow noreferrer">shared_ptr</a>. I have option here, that multiple edges will have pointers to one node object, but what will actually happen when I'll <code>erase</code> node from graph's unordered_map. Will I get false for last if/else condition if it was pointing there? I don't fully understand that.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Assume that I want to display the name of a node before it gets deleted. So I'll have something like that:</p>
  
  <pre><code>for(auto&amp; edge : graph.edges)
  {
      if(edge.from-&gt;to_delete)
      {
          printf("Node to delete: %s",edge.from-&gt;name.c_str());
          graph-&gt;nodes-&gt;erase(edge.from-&gt;name);
          edge.from = nullptr;
      }
      if(edge.to-&gt;to_delete)
      {
          printf("Node to delete: %s",edge.to-&gt;name.c_str());
          graph-&gt;nodes-&gt;erase(edge.to-&gt;name);
          edge.to = nullptr;
      }
  
      if(edge-&gt;from &amp;&amp; edge-&gt;to)
          DoSomethingWithNodes(edge-&gt;from, edge-&gt;to);
  }
  </code></pre>
  
  <p>Right now, there is no protection against null pointers, because as I've experienced <code>edge.from-&gt;to_delete</code> is somehow, sometimes returning true. What I've tried is changing if conditions to:</p>
  
  <pre><code>if(edge.from &amp;&amp; edge.from-&gt;to_delete)
  </code></pre>
  
  <p>but it doesn't help at all.</p>