title: returning an iterator from a function
body: <p>I have the following example:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;map&gt;
  #include &lt;conio.h&gt;
  
  typedef std::map&lt;int,int&gt; mi;
  typedef std::map&lt;int,int&gt;::iterator mit;
  
  mit myfind(mi mymap)
  {
      mit it = mymap.find(1);
      printf("in function: %d\n",it-&gt;second);
  
      return it;
  }
  
  void main()
  {
      mi a;
      a.insert(std::pair&lt;int,int&gt;(1,2));
      a.insert(std::pair&lt;int,int&gt;(3,4));
  
      mit it = myfind(a);
  
      printf("out of function: %d\n",it-&gt;second);
  
      _getch();
  
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <p>in function: 2</p>
  
  <p>out of function: -17891602</p>
  
  <p>Why? Does the iterator become invalid? Why? Thanks in advance.</p>