c++ - stringstream 删除前导 0

标签 c++ stringstream

在下面的代码中:

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/

相关文章:

c++ - C++中的互联网使用情况监控工具

c++ - 是否可以在片段着色器中写入特定的 mipmap 级别?

c++ - 使用自定义语言环境注入(inject) stringstream 时出现段错误

c++ - 用isalnum拆分字符串并存储到字符串 vector 中

c++ - 有像 std::size() 这样的函数吗?

c++ - 将 QProcess 用于 CLI

c++ - 为什么 [[no_unique_address]] 属性在某些情况下不起作用?

c++ - C++用stringstream将string转int并获取消耗的字符数

c++ - 为什么 `std::stringstream::stringstream(std::string&&)` 不存在?

c++ - 使用具有实时性能要求的 c++ STL stringstream 进行日志记录