C++ iterator to last element of a set
<p>I have the following code in C++</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;set&gt;

using namespace std;

int main()
{
    set&lt;int&gt; a;
    int n;

    for(int i=0;i&lt;3;i++){
      cin&gt;&gt;n;
      a.insert(n);
    }

    cout &lt;&lt; *a.end();
    return 0;
}
</code></pre>

<p>Why is it always printing "3" instead of the greatest element in the set? Replacing <code>cout &lt;&lt; *a.end();</code> with <code>cout &lt;&lt; *--a.end();</code> works fine.</p>