C++ cout格式化输出

本文详细介绍C++中IO流的各种格式化操作,包括如何保存和恢复cout的flag,如何控制bool值的输出形式,设置整数的进制,调整浮点数的输出精度和格式,以及如何进行输出填充。

转自:http://blog.youkuaiyun.com/dongfengkuayue/article/details/21229189

将 cout 的 flag 保存到变量, 以便修改后的恢复

ostream::fmtflags old = cout.flag() ; // 无参将返回当前 flag 值
cout.flag(old) ; // 恢复到原先保存的值


将 bool 值以 literals 输出

cout <<"numeric : " <<true <<" or " <<false <<endl ; // 1 or 0
cout <<"literals : " <<boolalpha <<true <<" or " <<false <<endl ; // true or false
cout <<"literals : " <<boolalpha <<0 <<endl ; // 0 原因: 0 在cout中不等价于 false

一旦我们使用 boolalpha 将改变 cout 对 bool 值的输出格式. 此后的 cout 都会将 bool 输出为 literals.


将 bool 值以 numeric 输出

cout <<"numeric : " <<noboolalpha <<true <<" or " <<false <<endl ;// 1 or 0

从此以后, cout 对 bool 值的输出将恢复 numeric 格式


指定 Integral Values 的 Base

const int ival = 17 ; // 'ival' is constant, so value never change
cout <<"oct : " <<oct <<ival <<endl ; // 21 : 8 进制
cout <<"dec : " <<dec <<ival <<endl ; // 17 : 10 进制
cout <<"hex : " <<hex <<ival <<endl ; // 11 : 16 进制
cout <<"hex : " <<hex <<17.01 <<endl ; // 17.01 : 不受影响
如 boolalpha 一样, oct, dec, hex 也是 persistent. 一旦改变, 将影响后续的输出格式.


显示表明 Integer Values 的 Base

复制代码
cout <<showbase ; // Show base when printing integral values
cout <<"oct : " <<oct <<ival <<endl ; // 21 : 8 进制
cout <<"dec : " <<dec <<ival <<endl ; // 017 : 10 进制
cout <<"hex : " <<hex <<ival <<endl ; // 0x11 : 16 进制
cout <<"hex : " <<hex <<17.01 <<endl ; // 17.01 : 不受影响
cout <<noshowbase ; // Reset state of the stream
复制代码

若想改变16进制字母的大小, 可以结合 uppercase/nouppercase

cout <<showbase <<uppercase ;
cout
<<"hex : " <<hex <<15 <<endl ; // 0XF 大写形式
cout <<nouppercase ;
cout
<<"hex : " <<hex <<15 <<endl ; // 0xf 小写形式

showbase 与 noshowbase 的作用周期也是 persistent


对于 float/double 型, 有三种格式化控制

一: 输出精度 precision : by default is 6pricision
   控制了至多一共会输出多少个数字.  
   当要输出的数字多余指定的值时, 将发生 四舍五入(rounded); 
   当要输出的数字少于指定的值时, 则实际输出的数字个数将少于指定值.
// cout.pricision(4) ; // 等价于 cout <<setprecision(4) ;
cout <<setprecision(4) <<12.345678 <<endl ; // 12.35 rounded!
cout <<setprecision(10) <<12.345678 <<endl ; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
cout <<cout.precision() <<endl ; // 输出当前精度
 二: 表现形式 notation : 'very large and very small values are printed using scientific notation. other values use fixeddecimal.'
   notation 控制了输出的形式 : 科学计数法(scientific) 和 定点小数(fixed)
float f = 101 / 6.0 ;
cout
<<fixed <<f <<endl ; // 16.83334 : 小数点后共6位
cout <<scientific <<f <<endl ; // 1.683333e+001 : 小数点后共6位
  恢复到初始状态
cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation
cout <<f <<endl ; // 16.8333 : 所有数字共6位
 三: 输出十进制浮点 'By default, when the fractional part of a floating-point value is 0, the decimal point is not displayed. The showpoint manipulator forces the decimal point ot be printed.'
cout <<10.0 <<endl ; // 10
cout <<showpoint <<10.0 <<endl ; // 10.0000
cout <<noshowpoint <<endl ; // Revert to default handling of decimal  


输出填充 
Padding the Output

   setw to specify the minimum space for the next numeric or string value.
cout <<setw(10) <<12.3 <<endl ; // ______12.3
cout <<setw(10) <<12 <<3 <<endl ; // ________123

cout
<<setw(3) <<12.345 <<endl ; // If the total output is more than 3, it can be extended
 
   left to left-justify the output.
cout <<left ; // left-justify
cout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___
 
   right to right-justify the output. Output is right-justified bu default.
cout <<right ; // By default
cout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___
 
   internal controls placement of the sign on negative value. internal left-justifies the sign and right-justifies the value, padding any intervening space with blanks.(if setfill not set) 
cout <<internal ; // By default
cout <<setw(5) <<-12 <<endl ; // 12___34___
 
   setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.
cout <<setfill('*') ; // By default
cout <<setw(5) <<12 <<endl ; // 12___34___
 

Header Files

   Manipulators Defined in <iomanip>
setfill(char ch) Fill whitespace with 'ch'
setprecision(
int n) Set floating-point precision to 'n'
setw(
int w) Read or write value to 'w' characters
setbase(
int b) Output integers in base 'b'(only 'b' is 8/10/16 could the function work)
 

### C++ 中 `cout` 的格式化输出方法 在 C++ 编程语言中,`std::cout` 是标准库中的流对象之一,用于向控制台输出数据。为了实现更复杂的格式化输出需求,可以借助 `<iomanip>` 头文件以及 `setf()` 和 `precision()` 等成员函数。 #### 1. 基本格式化设置 通过使用操纵符(manipulators),可以直接调整输出格式。以下是常见的操作: - **进制转换** 可以通过 `hex`, `dec`, 或 `oct` 来指定整数的输出进制。 ```cpp #include <iostream> using namespace std; int main() { cout << dec << 255 << endl; // 默认十进制输出 cout << hex << 255 << endl; // 十六进制输出 (ff) cout << oct << 255 << endl; // 八进制输出 (377) // 恢复默认状态 cout << resetiosflags(ios::basefield) << 255 << endl; return 0; } ``` - **浮点数表示形式** 浮点数可以通过 `fixed` 或 `scientific` 设置为固定小数位或科学记数法显示。 ```cpp #include <iostream> using namespace std; int main() { double value = 123.456; cout << fixed << value << endl; // 固定小数位输出 (123.456000) cout << scientific << value << endl; // 科学记数法输出 (1.234560e+02) // 恢复默认状态 cout << resetiosflags(ios::floatfield) << value << endl; return 0; } ``` #### 2. 使用 `<iomanip>` 提供的操作符 头文件 `<iomanip>` 定义了一系列用于精确控制输出格式的操纵器。 - **设置字段宽度 (`setw`)** 控制输出项占据的空间大小。 ```cpp #include <iostream> #include <iomanip> // setw, setfill using namespace std; int main() { cout << setw(10) << "hello" << endl; // 输出右对齐,占满 10 字符宽 cout << left << setw(10) << "world"; // 左对齐填充字符,默认为空格 return 0; } ``` - **设置填充字符 (`setfill`)** 当字段未填满时,可以用特定字符填补空白区域。 ```cpp #include <iostream> #include <iomanip> using namespace std; int main() { cout << setfill('*') << setw(10) << 123 << endl; // ***123*** return 0; } ``` #### 3. 成员函数方式 除了上述操纵符外,还可以调用 `std::ios_base` 类型的成员函数来修改输出行为。 - **精度控制 (`precision`)** 设定浮点数的小数位数。 ```cpp #include <iostream> using namespace std; int main() { double pi = 3.141592653589793; cout.precision(5); cout << fixed << pi << endl; // 显示到第 5 小数位 return 0; } ``` - **标志位管理 (`setf`/`unsetf`)** 修改输入输出流的行为模式,例如启用科学计数法、左对齐等。 ```cpp #include <iostream> using namespace std; int main() { cout.setf(ios::scientific); // 启用科学计数法 cout << 123.456 << endl; cout.unsetf(ios::scientific); // 关闭科学计数法 cout << 123.456 << endl; return 0; } ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值