给定以下代码:
/*Formatting Output
**Goal: practice using cout to format output to console
**Print the variables in three columns:
**Ints, Floats, Doubles
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 45;
float b = 45.323;
double c = 45.5468;
int aa = a + 9;
float bb = b + 9;
double cc = c + 9;
int aaa = aa + 9;
float bbb = bb + 9;
double ccc = cc + 9;
// 1st attempt :>
cout << "\n\n\n" << "// 1st attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << a << setw(15) << b << setw(15) << c << "\n";
cout << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
// 2nd attempt :>
cout << "\n\n\n" << "// 2nd attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << a << setw(15) << b << setw(15) << c << "\n";
cout << std::left << std::setfill(' ') << setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
// 3rd attempt :>
cout << "\n\n\n" << "// 3rd attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << a << setw(15) << b << setw(15) << c << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << a << setw(15) << b << setw(15) << c << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
return 0;
}
// https://repl.it/@Tredekka/Cpp-Understanding-stdsetw
...我得到以下输出:
gcc version 4.6.3
// 1st attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
// 2nd attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
4545.323 45.5468
54 54.323 54.5468
6363.323 63.5468
// 3rd attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
...注意:我故意与代码“不一致”,“因为”我试图理解<iomanip>
&&std::setw()
代码的行为。
如果您查看第一次尝试的输出,您会注意到标题行“字符串”输出与数据行“数字”输出偏移......虽然它“主要”实现了对齐列中的内容的目的,它既不准确也不一致。
在第二次尝试中,您会看到我发现如果我在行输出前添加:
<< std::left << std::setfill(' ') << setw(15)
...然后我使该行看起来正确(如标题和第二个数据行所示)...但是现在您会注意到第一个和第三个数据行非常错误:
4545.323 45.5468
...
6363.323 63.5468
...“使用/执行”如何...
<< std::left << std::setfill(' ') << setw(15)
...影响“未来”的执行setw()
?
为了完整起见,我在第三次尝试中展示了可以使用<iomanip>
&&正确和准确地对齐数据列(左或右) std::setw()
......但是为什么不一致?
(@WhozCraig 的回答帮助我到达了现在的位置,但也没有深入研究以帮助我理解:(a)为什么它“伪”有效||(b)为什么在你让它“第一次”正常工作之后,然后它会破坏“伪”功能。)