操纵符(域宽) ▣设置宽度:setw(宽度)或cout.width(宽度) intA[3][3]={{11,12,13},{21,22,23},31,32,33}}; for (int i=0;i<3;i++) for (int j=0;j<3;j++) cout<setw(4)<A[i][j];/∥设置输出宽度 ex07_setw.cpp for (int i=0;i<3;i++) for (int j=0;j<3;j++) { cout.width(4);/∥设置输出宽度 cout <A[i][j]; setw和cout.width只改变紧随其后的域,即只起一次作用 ·若数据超过设置的宽度,则自动扩展到所需最少宽度 7 http://math.ecnu.edu.cn/~jypan
http://math.ecnu.edu.cn/~jypan 操纵符(域宽) 7 设置宽度:setw(宽度) 或 cout.width(宽度) ► setw 和 cout.width 只改变紧随其后的域,即只起一次作用 ► 若数据超过设置的宽度,则自动扩展到所需最少宽度 int A[3][3]={{11,12,13},{21,22,23},{31,32,33}}; for (int i=0; i<3; i++) for (int j=0; j<3; j++) cout << setw(4) << A[i][j]; // 设置输出宽度 for (int i=0; i<3; i++) for (int j=0; j<3; j++) { cout.width(4); // 设置输出宽度 cout << A[i][j]; } ex07_setw.cpp
操纵符(填充符) 口设置填充符:setfill(字符)或cout.fill(字符) intA[3][3]={11,12,13},{21,22,23},{31,32,33}; /cout.fi11('*);/∥设置填充符 cout<setfi:11('*');/设置填充符 for(int i=0;i<3;i++) for(int j=0;j<3;j++) cout<setw(4)<A[i][j];/∥设置输出宽度 ex07_fill.cpp 缺省的填充符为空格 ·该命令的作用将一直保留,直到下次改变为止 8 http://math.ecnu.edu.cn/-jypan
http://math.ecnu.edu.cn/~jypan 操纵符(填充符) 8 设置填充符:setfill(字符) 或 cout.fill(字符) ► 缺省的填充符为空格 ► 该命令的作用将一直保留,直到下次改变为止 int A[3][3]={{11,12,13},{21,22,23},{31,32,33}}; // cout.fill('*'); // 设置填充符 cout << setfill('*'); // 设置填充符 for(int i=0; i<3; i++) for(int j=0; j<3; j++) cout << setw(4) << A[i][j]; // 设置输出宽度 ex07_fill.cpp
操纵符(对齐方式) 口设置对齐方式:left/right intA[3][3]={{11,12,13),{21,22,23},{31,32,33}; cout<1eft;/∥设置左对齐 for(int i=0;i<3;i++) { for(int j=0;j<3;j++) cout <setw(4)<<A[i][j]; cout <"\n"; ex07_left.cpp 缺省为右对齐 ·left/right的作用是持久的,直到遇到下一个同类型命令 9 http://math.ecnu.edu.cn/~jypan
http://math.ecnu.edu.cn/~jypan 操纵符(对齐方式) 9 设置对齐方式:left / right ► 缺省为右对齐 ► left/right 的作用是持久的,直到遇到下一个同类型命令 int A[3][3]={{11,12,13},{21,22,23},{31,32,33}}; cout << left; // 设置左对齐 for(int i=0; i<3; i++) { for(int j=0; j<3; j++) cout << setw(4) << A[i][j]; cout << "\n"; } ex07_left.cpp