在以下代码中:
stringstream ss; int a = 0; ss << str; ss >> a; cout << a;
如果为str = "05",stringstream将删除前导0,并打印5。如何避免这种情况?
str = "05"
stringstream
发布于 2018-02-23 21:38:40
您正在将字符串转换为int。int没有任何前导零的概念,它只是一个数字。如果您想在流中打印前导零,您可能会对setfill和setw操纵器感兴趣。如果a只有一位数长,下面的代码将打印前导0。
a
cout << setw(2) << setfill('0') << a;
https://stackoverflow.com/questions/48949037
相似问题