簡體   English   中英

使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM