使用std::cout和std::stringstream时,如何使符号出现在填充的末尾?

How can I make the sign appear at the end of the fill when using std::cout and std::stringstream?

我有以下代码用于将打印输出格式化为始终包含符号的 4 位数字:

std::stringstream pitch; 
pitch.precision(0);

pitch.width(4);
pitch.fill('0');

pitch << std::showpos << (int)(m_values["Pitch_1"]);

我也想显示符号("+"/"-"),但我希望它在填充之前,如下:

+002

但是,我这里的代码将“+”号移到了最重要的数字:

00+2

如果可能的话,我如何更改格式,以便使用前者而不是后者?

使用std::internal操纵器:

pitch << std::internal << std::showpos << 5;