我使用std :: ostringstream将double格式化为具有特定格式的字符串(使用撇号作为千位分隔符).但是,在某些情况下,ostringstream给了我与我的预期不同的结果.
据我所知,下面代码的预期输出应为"+01"; 相反,它输出"0 + 1".我在这里做错了什么,我怎样才能得到我需要的结果?
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
std::ostringstream stream;
stream << std::showpos; // Always show sign
stream << std::setw(3); // Minimum 3 characters
stream << std::setfill( '0' ); // Zero-padded
stream << 1; // Expected output: "+01"
std::cout << stream.str(); // Output: "0+1"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
c++ ×1