Namespaces
Variants

std::ios_base:: fmtflags

From cppreference.net
typedef /*implementation defined*/ fmtflags ;
static constexpr fmtflags dec = /*implementation defined*/

static constexpr fmtflags oct = /*implementation defined*/
static constexpr fmtflags hex = /*implementation defined*/

static constexpr fmtflags basefield = dec | oct | hex ;
static constexpr fmtflags left = /*implementation defined*/

static constexpr fmtflags right = /*implementation defined*/
static constexpr fmtflags internal = /*implementation defined*/

static constexpr fmtflags adjustfield = left | right | internal ;
static constexpr fmtflags scientific = /*implementation defined*/

static constexpr fmtflags fixed = /*implementation defined*/

static constexpr fmtflags floatfield = scientific | fixed ;
static constexpr fmtflags boolalpha = /*implementation defined*/

static constexpr fmtflags showbase = /*implementation defined*/
static constexpr fmtflags showpoint = /*implementation defined*/
static constexpr fmtflags showpos = /*implementation defined*/
static constexpr fmtflags skipws = /*implementation defined*/
static constexpr fmtflags unitbuf = /*implementation defined*/

static constexpr fmtflags uppercase = /*implementation defined*/

指定可用的格式化标志。这是一个 位掩码类型 。定义了以下常量:

常量 说明
dec 整数I/O使用十进制基数:参见 std::dec
oct 整数I/O使用八进制基数:参见 std::oct
hex 整数I/O使用十六进制基数:参见 std::hex
basefield dec | oct | hex 。用于掩码操作
left 左对齐(在右侧添加填充字符):参见 std::left
right 右对齐(在左侧添加填充字符):参见 std::right
internal 内部对齐(在内部指定点添加填充字符):参见 std::internal
adjustfield left | right | internal 。用于掩码操作
scientific 使用科学计数法生成浮点类型,若与 fixed 结合则使用十六进制表示法:参见 std::scientific
fixed 使用定点表示法生成浮点类型,若与 scientific 结合则使用十六进制表示法:参见 std::fixed
floatfield scientific | fixed 。用于掩码操作
boolalpha 以字母数字格式插入和提取 bool 类型:参见 std::boolalpha
showbase 为整数输出生成指示数字基数的前缀,在货币I/O中需要货币指示符:参见 std::showbase
showpoint 为浮点数输出无条件生成小数点字符:参见 std::showpoint
showpos 为非负数值输出生成 + 字符:参见 std::showpos
skipws 在某些输入操作前跳过前导空白字符:参见 std::skipws
unitbuf 每次输出操作后刷新输出:参见 std::unitbuf
uppercase 在某些输出操作中将特定小写字母替换为其大写形式:参见 std::uppercase

示例

以下示例展示了打印相同结果的几种不同方法。

#include <iostream>
int main()
{
    const int 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;     // 设置showbase
    std::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的格式化方式
(函数)
控制是否使用前缀指示数值基数
(函数)
在布尔值的文本表示和数值表示之间切换
(函数)
控制是否对非负数使用 +
(函数)
控制浮点表示中是否始终包含小数点
(函数)
控制每次操作后是否刷新输出
(函数)
控制输入时是否跳过前导空白符
(函数)
控制某些输出格式是否使用大写字符
(函数)