C++ setw:格式化输出(详解版)
720
720.0
720.00000000
7.2E+ 2
720.0
打印值的方式称为格式化。cout 对象具有格式化每种数据类型变量的标准方式。当然,有时候需要对数据显示的方式进行更多的控制。例如,下面程序显示了 3 行数字,每行之间都有空格。
// This program displays three rows of numbers. #include iostream using namespace std; int main() int num1 = 2897, num2 = 5, num3 = 837, num4 = 34, num5 = 7, num6 = 1623, num7 = 390, num8 = 3456, num9 = 12; // Display the first row of numbers cout num1 num2 num3 endl; // Display the second row of numbers cout num4 num5 num6 endl; // Display the third row of numbers cout num7 num8 num9 endl; return 0; }
程序输出结果:
2897 5 837
34 7 1623
390 3456 12
美中不足的是,数字未能按列对齐。这是因为一些数字(如 5 和 7)在屏幕上占据一个位置,而其他数字占据两到三个位置。cout 仅使用打印每个数字所需的空格数。
为了弥补这一点,cout 提供了一种指定每个号码使用的最小空格数量的方法。流操作符 setw 可用于建立指定宽度的打印区域。以下是其用法示例:
value = 23;
cout setw(5) value;
单词 setw 后面括号内的数字表示紧随其后的值的字段宽度。字段宽度是屏幕上打印值的字符位置或空格的最小数量。在此示例中,数字 23 将显示在 5 个空格的字段中。
为了进一步解释其工作原理,请看以下语句:
value = 23;
cout ( setw(5) value )
这将产生以下输出:
( 23)
请注意,这个数字占据了字段的最后两个位置。由于这个数字没有使用整个字段,所以 cout 用空格填充了额外的 3 个位置。因为这个数字出现在字段的右侧,空格 填充 在前面,所以它被认为是右对齐的。
下面的程序显示了如何通过使用 setw 来将之前程序中的数字打印在完美排列的列中。另外,由于程序使用 setw(6),最大的数字有 4 位,所以数字将被分开,而不必在数字之间打印一个包含空格的字符串文字。
// This program uses setw to display three rows of numbers so they align. #include iostream #include iomanip // Header file needed to use setw/ using namespace std; int main() int num1 = 2897, num2 = 5, num3 = 837, num4 = 34, num5 = 7, num6 = 1623, num7 = 390, num8 = 3456, num9 = 12; // Display the first row of numbers cout setw(6) num1 setw(6) num2 setw(6) num3 endl; //Display the second row of numbers cout setw(6) num4 setw(6) num5 setw(6) num6 endl; // Display the third row of numbers cout setw(6) num7 setw(6) num8 setw(6) num9 endl; return 0; }
程序输出结果:
2897 5 837 34 7 1623 390 3456 12
注意,在程序第 3 行的 #include 指令中命名了一个新的头文件 iomanip。该文件必须包含在使用 setw 的任何程序中。
请注意,setw 操作符要与每个值一起使用,这是因为 setw 只为紧随其后的值建立一个字段宽度。打印该值后,cout 将回到其默认的打印方式。如果数字太大导致字段无法完全容纳,那会怎么祥呢?如下列语句所示:
value = 18397;
cout setw(2) value;
在这种情况下,cout 将打印整个数字,因为 setw 只指定了打印字段中最少的位置数。任何需要比指定最小值大的字段的数字将导致 cout 覆盖 setw 值。
可以为任何类型的数据指定字段宽度。下面的程序显示了与整数、浮点数及字符串对象一起使用的 setw。
// This program demonstrates the setw manipulator //being used with variables of various data types. #include iostream #include iomanip // Header file needed to use setw #include string // Header file needed to use string objects using namespace std; int main() int intValue = 3928; double doubleValue = 91.5; string stringValue = Jill Q. Jones cout ( setw (5) intValue ) endl; cout ( setw (8) doubleValue ) endl; cout ( setw (16) stringValue ) endl; return 0; }
程序输出结果:
( 3928) ( 91.5) ( Jill Q. Jones)
此程序说明了一些要点:
默认情况下,该字段中打印的值为右对齐。这意味着它与打印字段的右侧对齐,并且必要时在值前填充空格。
相关文章
- java的输入和输出语句_c++输入输出语句
- C++ 炼气期之数据是主角
- C++系列笔记(十一)
- 浅析C++中的this指针[通俗易懂]
- C++中voliate关键字
- 数字逆序输出 -- C++ 实现
- c++获取子类窗口句柄位置_C++中各种获取窗口句柄的方法「建议收藏」
- c++ auto类型_auto C++
- C++友元函数与友元类
- c++cstring头文件_fstream头文件
- C/C++条件运算符你不知道的那些事[通俗易懂]
- C++字符串加密_c++字符串连接函数
- C++-编写类String的构造函数、析构函数和赋值函数
- c++-求输出结果
- C/C++语言常用排序算法
- C/C++ 获取操作系统版本
- c++基础篇之C++ 模板
- C++重载<<和>>(C++重载输出运算符和输入运算符)
- Linux下C编程:实现更多可能性(linux c++程序)
- 解析C++浮点数的格式化输出
- C++十进制转换为二进制的实例代码
- VC++实现输出GIF到窗体并显示GIF动画的方法