title: c++ map/set iterator not dereferencable --- map<const char *, string>
body: <pre><code>class A
  {
  public:
      map&lt;string, string&gt; strs; // it's initialized in a function
      map&lt;const char *, string&gt; myMap;
      virtual void setMyMap()
      {
          myMap.insert({str.begin()-&gt;first.c_str(), "aaa"});
      }
  };
  
  class B : public class A
  {};
  
  class C : public class B
  {
      virtual void setMyMap()
      {
          B::setMyMap();
          for (auto it = myMap.begin(); it != myMap.end(); ++it)
          {
              cout &lt;&lt; it-&gt;first &lt;&lt; std::endl; // "a"
          }
          cout&lt;&lt;(myMap.find("a") == myMap.end()); // 1
          myMap.find("a")-&gt;first; // get the error
      }
  };
  </code></pre>
  
  <p>Here is my code.<br>
  I get the error <code>map/set iterator not dereferencable</code> in the function <code>setMyMap</code> in the class <code>C</code>.<br>
  I get this error because <code>myMap.find("a") == myMap.end()</code> is <code>true</code>.<br>
  But I don't know why. To my surprise, I just print this map in the same function and I can see all of the <code>first</code> of this map. Here I can see <code>a</code>. I really don't know why I try to find the same <code>first</code> but failed. <code>a</code> is definitely in the map. Otherwise I can't print it with that <code>for</code>.</p>