c++ - 如何在 C++ 中格式化输出

标签 c++

int var = 0;
cout << var;
// the output would be "0"

但是如果我想输出“00”,我应该怎么做呢?

最佳答案

int var = 0;
std::cout << std::setw(2) << std::setfill('0') << var << std::endl;

一个更有趣的:

int var = 458;
std::cout << std::setw(8) << std::setfill('0') << var << std::endl;

关于c++ - 如何在 C++ 中格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893356/

相关文章:

c++ - 数组还是 vector ?

c++ - 函数末尾的断点

C++ move aligned_storage数组元素的正确方法

c++ - SetFocus() 因有效的窗口句柄而失败

c++ - 为什么不能使用函数的 typedef 来定义函数?

c++ - 双映射结构实现?

c++ - 编辑开源库

c++ - 如何避免转换运算符调用复制构造函数?

c++ - 在哪里为 QMainWindow : in header file or in constructor? 声明某些 Qt 对象

c++ - 使用 Boost Variants 在 C++ 中表示 JSON 数据