std::ostringstream puts sign in wrong location? : <p>I am using std::ostringstream to format a double to a string with a specific format (using apostrophes as thousands separators). However, in some cases ostringstream gave me a different result from what I expected. </p>

<p>As far as I can tell, the expected output of the code below should be "+01"; instead it outputs "0+1". What am I doing wrong here, and how can I get the result I need?</p>

<pre><code>#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;

int main() 
{
    std::ostringstream stream;
    stream &lt;&lt; std::showpos; // Always show sign
    stream &lt;&lt; std::setw(3); // Minimum 3 characters
    stream &lt;&lt; std::setfill( '0' ); // Zero-padded
    stream &lt;&lt; 1; // Expected output: "+01"

    std::cout &lt;&lt; stream.str(); // Output: "0+1"
    return 0;
}
</code></pre>

<p><a href="https://ideone.com/BvHzrJ" rel="noreferrer">Code on ideone</a></p>
