std::ios_base::fmtflags

来自cppreference.com
< cpp‎ | io‎ | ios base
 
 
 
 
typedef/* 由实现定义 */ fmtflags;
staticconstexpr fmtflags dec =/* 由实现定义 */

staticconstexpr fmtflags oct =/* 由实现定义 */
staticconstexpr fmtflags hex =/* 由实现定义 */

staticconstexpr fmtflags basefield = dec | oct | hex;
staticconstexpr fmtflags left =/* 由实现定义 */

staticconstexpr fmtflags right =/* 由实现定义 */
staticconstexpr fmtflags internal =/* 由实现定义 */

staticconstexpr fmtflags adjustfield = left | right | internal;
staticconstexpr fmtflags scientific =/* 由实现定义 */

staticconstexpr fmtflags fixed =/* 由实现定义 */

staticconstexpr fmtflags floatfield = scientific | fixed;
staticconstexpr fmtflags boolalpha =/* 由实现定义 */

staticconstexpr fmtflags showbase =/* 由实现定义 */
staticconstexpr fmtflags showpoint =/* 由实现定义 */
staticconstexpr fmtflags showpos =/* 由实现定义 */
staticconstexpr fmtflags skipws =/* 由实现定义 */
staticconstexpr fmtflags unitbuf =/* 由实现定义 */

staticconstexpr fmtflags uppercase =/* 由实现定义 */

指定可用的格式化标志。它是位掩码类型(BitmaskType) 。定义了下列常量:

常量 解释
dec 为整数输入/输出使用十进制底:见 std::dec
oct 为整数输入/输出使用八进制底:见 std::oct
hex 为整数输入/输出使用十六进制底:见 std::hex
basefielddec | oct | hex。适用于掩码运算
left 左对齐(添加填充字符到右侧):见 std::left
right 右对齐(添加填充字符到左侧):见 std::right
internal 居中对齐(添加填充字符到内部选定点):见 std::internal
adjustfieldleft | right | internal。适用于掩码运算
scientific 用科学记数法生成浮点数类型,或在与 fixed 组合时用十六进制记法:见 std::scientific
fixed 用定点记法生成浮点数类型,或在与 scientific 组合时用十六进制记法:见 std::fixed
floatfieldscientific | fixed。适用于掩码运算
boolalpha 以字母数字格式插入并提取 bool 类型:见 std::boolalpha
showbase 生成为整数输出指示数字基底的前缀,货币输入/输出中要求现金指示符:见 std::showbase
showpoint 无条件为浮点数输出生成小数点字符:见 std::showpoint
showpos 为非负数值输出生成 + 字符:见 std::showpos
skipws 在具体输入操作前跳过前导空白:见 std::skipws
unitbuf 在每次输出操作后冲洗输出:见 std::unitbuf
uppercase 在具体输出的输出操作中以大写等价替换小写字符:见 std::uppercase

[编辑]示例

下列示例展示打印同一结果的数种不同方式。

#include <iostream>   int main(){constint num =150;   // 以 fmtflags 为类成员常量:std::cout.setf(std::ios_base::hex, std::ios_base::basefield);std::cout.setf(std::ios_base::showbase);std::cout<< num <<'\n';   // 以 fmtflags 为继承的类成员常量:std::cout.setf(std::ios::hex, std::ios::basefield);std::cout.setf(std::ios::showbase);std::cout<< num <<'\n';   // 以 fmtflags 为对象成员常量:std::cout.setf(std::cout.hex, std::cout.basefield);std::cout.setf(std::cout.showbase);std::cout<< num <<'\n';   // 以 fmtflags 为类型: std::ios_base::fmtflags ff; ff =std::cout.flags(); ff &= ~std::cout.basefield;// 解除设置 basefield 位 ff |=std::cout.hex;// 设置 hex ff |=std::cout.showbase;// 设置 showbasestd::cout.flags(ff);std::cout<< num <<'\n';   // 不使用 fmtflags,但使用操纵符:std::cout<<std::hex<<std::showbase<< num <<'\n';}

输出:

0x96 0x96 0x96 0x96 0x96

[编辑]参阅

管理格式标志
(公开成员函数)[编辑]
设置特定格式标志
(公开成员函数)[编辑]
清除特定格式的标志
(公开成员函数)[编辑]
更改用于整数 I/O 的基数
(函数)[编辑]
更改填充字符
(函数模板)[编辑]
更改用数于浮点数 I/O 的格式化
(函数)[编辑]
控制是否使用前缀指示数值基数
(函数)[编辑]
在布尔值的文本和数值表示间切换
(函数)[编辑]
控制是否将 + 号与非负数一同使用
(函数)[编辑]
控制浮点数表示是否始终包含小数点
(函数)[编辑]
控制是否每次操作后冲洗输出
(函数)[编辑]
控制是否跳过输入上的前导空白符
(函数)[编辑]
控制一些输出操作是否使用大写字母
(函数)[编辑]
close