在下面的代码中:
stringstream ss;
int a = 0;
ss << str;
ss >> a;
cout << a;
如果 str = "05"
,stringstream
删除前导 0,并打印 5。
如何避免这种情况?
最佳答案
您正在将字符串转换为整数。 int 没有任何前导零的概念,它只是一个数字。如果您想在流中打印前导零,您可能会对 setfill 感兴趣和 setw操纵者。如果 a
只有一个数字长,下面的代码将打印前导 0。
cout << setw(2) << setfill('0') << a;
关于c++ - stringstream 删除前导 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48949037/