for文に含まれているかどうか、または単純なcout << '*'
が3回繰り返された場合、またはcout << "***"
の場合は、この異なる動作がなぜ発生しますか?setw、coutとの再生
すべての文章にはダイヤモンドの2行目と8行目のようなsetw(6)がありますが、すべてが同じ動作をしているわけではありません。いくつかの出力パディング6スペース、他の統計出力パディング4スペース。また
*
***
*****
*******
*********
*******
*****
***
*
cout << "***"
***
for x3 cout << '*'
***
cout << '*' three times
***
これは、されていない:
#include <iostream>
using std::cout;
#include <iomanip>
using std::setw;
int main(){
int ast = 1,
pos = 5;
for(int i = 1; i <= 9; i++) {
cout << setw(pos);
for(int j = 1; j <= ast; j++)
cout << '*';
if(i <= 4) {
ast += 2;
pos -= 1;
}
else {
ast -= 2;
pos += 1;
}
cout << '\n';
}
cout << '\n';
cout << "cout << \"***\"\n";
cout << setw(6);
cout << "***";
cout << '\n';
cout << "for x3 cout << '*'\n";
cout << setw(6);
for(int i = 1; i <= 3; i++) // this should look like the first for statement, but not
cout << '*';
cout << '\n';
cout << "cout << '*' three times\n";
cout << setw(6);
cout << '*';
cout << '*';
cout << '*';
cout << '\n';
return 0;
}
これは私がこのコードを持って出力され
cout << setw(6);
for(int i = 1; i <= 3; i++)
cout << '*';
これと同じ?
cout << setw(6);
cout << '*';
cout << '*';
cout << '*';
ありがとうございます。