C++ Iomanip lib setfill and setw issues
C++ Iomanip lib setfill and setw issues
我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int time;
int honolulu, seattle, london, moscow, hongkong, auckland;
cout << "What is the current time in Philadelphia? ";
cin >> time;
honolulu = (time+2400-600)%2400;
seattle = (time+2400-300)%2400;
london = (time+2400+500)%2400;
moscow = (time+2400+800)%2400;
hongkong = (time+2400+1200)%2400;
auckland = (time+2400+1700)%2400;
cout << endl << "Current times in other cities: " << endl;
cout << setw (12) << left << "Honolulu:";
cout << setw (4) << setfill('0') << honolulu << endl;
cout << setw (12) << left << "Seattle:";
cout << setw (4) << setfill('0') << seattle << endl;
cout << setw (12) << left << "London:";
cout << setw (4) << setfill('0') << london << endl;
cout << setw (12) << left << "Moscow:";
cout << setw (4) << setfill('0') << moscow << endl;
cout << setw (12) << left << "Hong Kong:";
cout << setw (4) << setfill('0') << hongkong << endl;
cout << setw (12) << left << "Auckland:";
cout << setw (4) << setfill('0') << auckland << endl;
return 0;
}
所需输出:
What is the current time in Philadelphia? 0415
Current times in other cities:
Honolulu: 2215
Seattle: 1150
London: 9150
Moscow: 1215
Hong Kong: 1615
Auckland: 2115
我的输出:
What is the current time in Philadelphia? 0415
Current times in other cities:
Honolulu: 2215
Seattle:00001150
London:000009150
Moscow:000001215
Hong Kong:001615
Auckland:0002115
我做错了什么?输出的第一行Honolulu: 2215
是正确的。但接下来的几行有前导零。我不明白为什么会发生这种事?我的代码有问题吗?还是我误解了函数setfill
和setw
的工作方式?
填充字符是"粘性"的,因此它在您更改之前一直有效。
在您的情况下,您希望0
作为数字字段的填充,但空格作为字符字段的填充。因此,您必须显式设置,类似于以下内容:
cout << setfill(' ') << setw (12) << left << "Seattle:";
许多iomanip
对象都是"粘性"的,也就是说,它们粘在流上并影响后续的行。
当你有这个:
cout << setw (12) << left << "Seattle:";
cout << setw (4) << setfill('0') << seattle << endl;
这将使CCD_ 6对于下一行保持活动。所以你可能更喜欢
cout << setw (12) << setfill(' ') << left << "Seattle:";
cout << setw (4) << setfill('0') << seattle << endl;
正如其他评论中提到的,许多I/O操纵器都是"粘性"的。我个人更喜欢使用RAII:来解决这类问题
class stream_format_raii {
public:
stream_format_raii(std::ostream &stream)
: stream_(stream)
, state_(NULL) {
state_.copyfmt(stream_);
}
~stream_format_raii() {
stream_.copyfmt(state_);
}
public:
std::ostream &stream_;
std::ios state_;
};
这个类所做的是在构建时备份当前流的格式,并在销毁时将其设置回。
你可以这样使用它:
void printCity(std::ostream &os, const std::string name, int time) {
stream_format_raii back(os);
os << std::setw(12) << std::left << (name + ":");
os << std::setw(4) << std::setfill('0') << time;
}
int main() {
// Same as before
printCity(std::cout, "Honolulu", honolulu);
// Same as before
}
相关文章:
- Issues with Win32 ReadProcessMemory API
- 如何使用 setfill 和 setw 在字符串变量中存储十六进制值
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 如何在 c++ 的 toString 方法中使用 setw(x) 并将其放入变量或建议中?
- Zooming Issues with glutMouseWheelFunc
- 在c++中使用setw和setprecision时,如何显示带值的$符号
- C++ cout 列表,其中小数点使用 setw(x) 对齐,而不是put_money
- Cygwin pkg-config/protobuf filepath issues
- 为什么 std:: setw 和 std::hex 不适合下面的代码?
- 如何理解 C++ std::setw 的不一致行为?
- 使用setW()函数保存文件时,如何获取每个属性的值
- 使用setW和setFill的输出格式
- STD :: SETW,STD :: SETFILL等的真实返回类型是什么?
- 格式输出C 带有setW
- C :SETW()仅在第一行,循环中工作
- std::setfill 和 std::setw 用于输入流?
- SETW在第一次循环后提供了额外的空间
- SETW不是功能
- 将setw()作为变量放置
- C++ Iomanip lib setfill and setw issues