使用时声明: #include <iostream> using namespace std; #include <iomanip> using std::setw; cout<<'s'< <setw(8)<<'a'<<endl; 则在屏幕显示 s a //s与a之间有7个空格, 上代码: #include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { cout << "Element" << setw( 13 ) << "Value 0; i < 10; i++ ) { n[ i ] = i + 100; // 设置元素 i 为 i + 100 } cout << "Element" << setw ; // 输出数组中每个元素的值 for ( int j = 0; j < 10; j++ ) { cout << setw
(10) << "学号" << setw(10) << "性别" <<setw(10) << "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw ) <<L.elem[i].Num << setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15) << L.elem (10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw (10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw (10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw
(31) << str0 << setw(13) << str0 << endl; cout <<setw(30)<< str1 <<setw(19)<<str2<<setw(9)<<str3< < endl; cout << setw(24) << str4 <<setw(19)<<str4<< endl; cout << setw(24) << str4 << setw(11 << setw(24) << str4 << setw(9) << str6 << setw(11) << str7 << setw(5) << str4 << endl; cout << setw(24) << str4 << setw(19) << str4 << endl; cout << setw(24) << str4 << setw(19) << str4 << endl setw(30) << str4 <<setw(4)<<str18<<setw(4)<<str18<<setw(9)<<str4<<setw(4)<<str18 <<setw(4)<<
for (int i=0;i<320;i++) { myfile<<"AH["<<setw(3)<<i<<"] = "<<<em>setw</em>(6)<<AH[i]<<endl; (9)<<vision[i]<<","; } myfile<<endl; myfile<<"展开列数:"<<endl; for (int i1=0;i1<40;i1++) { myfile<<setw( (5)<<"行号"<<setw(16)<<"视野宽度"<<setw(16)<<"展开列数"<<setw(16)<<"矫正系数"<<endl; cout<<"展开列数:"<<endl; for (int (5)<<i<<setw(16)<<vision[i]<<setw(16)<<sizeofRow[i]<<setw(16)<<(int)(1000*sizeofRow[i]/column)<<endl; cout<<setw(9)<<(int)sizeofRow[i]<<","; } cout<<endl; WriteExternArray(); } #endif } 2009-8-27 书于 武汉大学
(40); cout << "**************" << endl; cout << setw(40); cout << "*图书管理系统*" << endl; cout << setw(40); cout << "**************" << endl; } void ShowRootMenu() { cout << setw (40); cout << "请选择功能:" << endl; cout << endl; cout << setw(38); cout << "1 添加新书" << endl; cout << endl; cout << setw(38); cout << "2 浏览全部" << endl; cout << endl; (5) << "Index"; cout << setw(22) << "Name" << setw(22) << "Isbn"; cout << setw(15) << "Price
a-b-c-d; cout<<"50只动物随机分为五个组"<<endl; cout<<"A组"<<" B组"<<" C组"<<" D组"<<" E组"<<endl; cout<<setw (2)<<a<<" "<<setw(2)<<b<<" "<<setw(2)<<c<<" "<<setw(2)<<d<<" "<<setw(2)<<e<<endl; system("pause
(4) << 12 << setw(12) << -12 << endl; 07 cout << dec << setw(4) << 12 << setw(12) << -12 << endl; ); 10 cout << hex << setw(4) << 12 << setw(12) << -12 << endl; 11 cout << dec << setw(4) << 12 << setw(12) << -12 << endl; 12 cout << oct << setw(4) << 12 << setw(12) << -12 << endl; 13 return 0 (4) << hex << 32 << setw(4) << oct << 32 << endl; 6 cout << noshowbase << setw(4) << hex << 32 << setw (8) << "NAME" << right << setw(8) << "ID"; 13 cout << right << setw(8) << "COUNT" << right << setw(
setw(n) 设置字段宽度为n位。 setiosflags(ios::fixed) 设置浮点数以固定的小数位数显示。 width(n) setw(n) 设置字段宽度为n位。 fill(c) setfill(c) 设置填充字符c。 stderr 以小数形式,保留三位小数输出: cout<<setprecision(3)<<setiosflags(ios::fixed)<<3.1415926<<endl; C++输出流的格式控制setw (int _w) { return SMANIP(int)(__setw, _w); } //他们都是inline 内联函数 (1)setw(int n) 预设输出宽度 如:cout<<setw(6)< setw(int _w)讲解 << setw(int _w) << 输出位宽 如<< setw(7) << 1000 输出结果为 "1000 "(紧跟三个空格) 而如<< setw(3) << 1000
(31) << str0 << setw(13) << str0 << endl; cout <<setw(30)<< str1 <<setw(19)<<str2<<setw(9)<<str3< < endl; cout << setw(24) << str4 <<setw(19)<<str4<< endl; cout << setw(24) << str4 << setw(11 << setw(24) << str4 << setw(9) << str6 << setw(11) << str7 << setw(5) << str4 << endl; cout << setw(26) << str4 << endl; cout << setw(28) << str4 << setw(30) << str12 << endl; cout << setw setw(30) << str4 <<setw(4)<<str18<<setw(4)<<str18<<setw(9)<<str4<<setw(4)<<str18 <<setw(4)<<
(12) << setfill(' ') << "名字" << left << setw(12) << setfill(' ') << "年龄" << left << setw(12) << setfill (' ') << "性别" << left << setw(12) << setfill(' ') << "电话" << left << setw(12) << setfill(' ') << "地址" [i].name; cout << left << setw(12) << setfill(' ') << pc->data[i].age; cout << left << setw(12) < (' ') << "性别" << left << setw(12) << setfill(' ') << "电话" << left << setw(12) << setfill(' ') << "地址" << endl; cout << left << setw(12) << setfill(' ') << pc->data[pos].name; cout << left << setw(12
setw()函数 在C++中,setw(int n)用来控制输出间隔。 ? 则在屏幕显示 ? //s与a之间有7个空格,setw()只对其后面紧跟的输出产生作用,如上例中,表示’a’共占8个位置,不足的用空格填充。若输入的内容超过setw()设置的长度,则按实际长度输出。 setw()默认填充的内容为空格,可以用setfill()配合使用设置其他字符填充。 即cout <<setw(2) <<a <<b;语句中域宽设置仅对a有效,对b无效。 setw和setfill被称为输出控制符,使用时需要在程序开头写上#include “iomanip.h”,否则无法使用。 实例 ? 上面的程序使用了 setw() 函数来格式化输出。 结果: ?
(31) << str0 << setw(13) << str0 << endl; cout <<setw(30)<< str1 <<setw(19)<<str2<<setw(9)<<str3< < endl; cout << setw(24) << str4 <<setw(19)<<str4<< endl; cout << setw(24) << str4 << setw(11 << setw(24) << str4 << setw(9) << str6 << setw(11) << str7 << setw(5) << str4 << endl; cout << setw(26) << str4 << endl; cout << setw(28) << str4 << setw(30) << str12 << endl; cout << setw setw(30) << str4 <<setw(4)<<str18<<setw(4)<<str18<<setw(9)<<str4<<setw(4)<<str18 <<setw(4)<<
print universal-time format (HH:MM:SS) void Time::printUniversal() const { cout << setfill('0') << setw (2) << this->hour << ":" << setw(2) << this->minute << ":" << setw(2) << this->second; } //print standard-time 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << setw(2)
注意到C++的输出控制格式,要包含头文件iomanip,用到setw()控制位宽,setfiil()选择填充的字符。 >>month>>day; } }; void Display(CDate & date,CTime & time) { cout<<date.year<<'-'<<setfill('0')<<setw (2)<<date.month<<'-'<<setw(2)<<date.day<<' '<<setw(2)<<time.hour<<':'<<setw(2)<<time.min<<':'<<setw(2
<<endl; cout << endl; cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << << endl; cout << endl; cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) < << endl; cout << endl; cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) < << endl; cout << endl; cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) < << endl; cout << endl; cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) <
如setiosflags、setw、setfill、setprecision、hex、oct等。 (一)、常用的流操纵算子: ? (二)、ios类的枚举常量 ? << "=================宽度控制=====================" << endl; cout << n << '#' << endl; cout << <em>setw</em> // 对齐控制会影响下一个输出 //cout<<<em>setw</em>(10)<<setiosflags(ios::right)<<n<<'#'<<endl; cout << <em>setw</em>(10) << << n << '#' << endl; cout << <em>setw</em>(10) << n << '#' << endl; // 填充控制会影响下一个输出 cout << <em>setw</em>(10) << setfill(' ') << n << '#' << endl; cout << "=================精度控制====================
query.store(); cout << "记录条数: " << res.size() << endl << endl; // 打印表中的数据 cout.setf(ios::left); cout << setw (20) << "Item" << setw(9) << "Num" << setw(9) << "Weight" << setw(9) << "Price" << "Date" << endl = res.end(); ++i) { row = *i; // 可以使用 列的位置或者 名称来 显示 cout << setw(20) << row[0].c_str() << setw (9) << row[1].c_str() << setw(9) << row.lookup_by_name("weight").c_str() << setw(9) << row[3].c_str
TelNo; 16 } 17 void disp() 18 { 19 cout<<setiosflags(ios::left) 20 <<setw (6)<<age<<setw(10)<<name<<setw(7)<<TelNo<<endl; 21 } 22 char *getname() 23 { 24 (6)<<"年龄"<<setw(10)<<"姓名"<<setw(7)<<"电话"<<endl; 50 input.read((char *)&s,sizeof(s)); 51 while 64 cout<<"输入要查询的姓名(可只输入姓氏):"; 65 cin>>sname; 66 cout<<"输出查询结果:"<<endl; 67 cout<<setw (6)<<"年龄"<<setw(10)<<"姓名"<<setw(6)<<"电话"<<endl; 68 for(int i=0;i<n;i++) 69 { 70 file.read
(20)<<"BinaryRecursion("<<c<<"):"; BinaryRecursion(c); cout<<endl; cout<<setw(20) <<"BinaryVector("<<c<<"):"; BinaryVector(c); cout<<setw(20)<<"Binarycout("<<c<<"):"; "<<c<<"):"; BinaryVector(c); cout<<setw(20)<<"Binarycout("<<c<<"):"; Binarycout(c); cout<<setw( 指定数据输出宽度:用C++提供的函数setw()指定输出数据项的宽度。setw()括号中通常给出一个正整数值,用于限定紧跟其后的一个数据项的输出宽度。 如:setw(8)表示紧跟其后的数据项的输出占8个字符宽度。setw()只能限定紧随其后的一个数据项,输出后即回到默认输出方式。
(5) << "n" << setw(15) << "log2(n)" << setw(10) << "n" << setw(15) << " n log2(n)" << setw(10) << "n^2" << setw(15) << "2^n" << setw(15) << "n! ,n太大时会溢出 cout << setw(5) << n << setw(15) << fixed << setprecision(2) << logVal << setw(10) << n << setw(15) << fixed << setprecision(2) << nLogN (15) << expVal; else cout << setw(15) << "溢出"; if (n <= 20) cout << setw(15)