したがって、課題は、この出力を使用してプログラムを作成することです。
000042
420000
42
-42-
私の最初の試みは次のようなものでした:
int fortyTwo = 42;
cout << setfill('0') << setw(6) << fortyTwo << endl;
cout << fortyTwo << setfill('0') << setw(6) << endl;
cout << fortyTwo << endl;
cout << setfill('-') << setw(4) << fortyTwo << setfill('-') << endl;
これは私に次のようなものを与えました:
000042
42
000042
42-- (sometimes just -42)
著者の解決策は次のとおりです。
cout << setfill('0') << setw(6) << 42 << endl;
cout << left << setw(6) << 42 << endl;
cout << 42 << endl;
cout << setfill('-') << setw(4) << -42 << endl;
著者が setfill を 1 回しか使用しないのはなぜですか? setfill は最初の 2 行ではどのように機能しますが、3 行目で突然停止しますか? -42 の前に setfill('-') と setw(4) を配置すると、--42 ではなく -42- が生成されるのはなぜですか? 左揃え演算子は何に必要ですか?
最後に、私のバージョンが正しい出力を生成しないのはなぜですか?