title: Detect if iterator is last element of std::map
body: <p>This is similar to <a href="https://stackoverflow.com/questions/289715/last-key-in-a-stdmap">this question</a> but not a duplicate. I'm trying to iterate through a map and print the values of each element, but with slightly different output on the last element. In that question, they recommend using <code>map.rbegin().base()</code>, but it's not working for me.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  int main()
  {
      std::map &lt;char, int&gt; charMap = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4} };
      for (auto iter = charMap.begin(); iter != charMap.end(); iter++)
      {
          std::cout &lt;&lt; iter-&gt;first &lt;&lt; ":\t" &lt;&lt; iter-&gt;second;
          if (iter == charMap.rbegin().base())
              std::cout &lt;&lt; "\t//This is the last element.\n";
          else
              std::cout &lt;&lt; "\n";
      }
  }
  </code></pre>
  
  <p>I'd expect my output to look like this:</p>
  
  <pre><code>a:    1
  b:    2
  c:    3
  d:    4    //This is the last element. 
  </code></pre>
  
  <p>But instead, I am getting this output:</p>
  
  <pre><code>a:    1
  b:    2
  c:    3
  d:    4    
  </code></pre>
  
  <p>Now I realize that there is a better way to do this, but I would expect this to work also. Why can't I compare <code>iter</code> and <code>charMap.rbegin().base()</code>?</p>