std::ios_base::setf
来自 cppreference.cn
fmtflags setf( fmtflags flags ); |
(1) | |
fmtflags setf( fmtflags flags, fmtflags mask ); |
(2) | |
将格式化标志设置为指定的设置。
1) 设置由 flags 标识的格式化标志。 实际上,执行以下操作 fl = fl | flags 其中
fl
定义了内部格式化标志的状态。2) 清除 mask 下的格式化标志,并将清除的标志设置为由 flags 指定的标志。 实际上,执行以下操作 fl = (fl & ~mask) | (flags & mask) 其中
fl
定义了内部格式化标志的状态。目录 |
[编辑] 参数
flags, mask | - | 新的格式化设置。 mask 定义了可以更改哪些标志,flags 定义了要更改的标志中应设置哪些标志(其他标志将被清除)。 两个参数都可以是格式化标志常量的组合 |
[编辑] 格式化标志
常量 | 解释 |
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 <iomanip> #include <iostream> #include <numbers> int main() { const double PI = std::numbers::pi; const int WIDTH = 15; std::cout.setf(std::ios::right); // equivalent: cout << right; std::cout << std::setw(WIDTH / 2) << "radius" << std::setw(WIDTH) << "circumference" << '\n'; std::cout.setf(std::ios::fixed); // equivalent: cout << fixed; for (double radius = 1; radius <= 6; radius += 0.5) std::cout << std::setprecision(1) << std::setw(WIDTH / 2) << radius << std::setprecision(2) << std::setw(WIDTH) << (2 * PI * radius) << '\n'; }
输出
radius circumference 1.0 6.28 1.5 9.42 2.0 12.57 2.5 15.71 3.0 18.85 3.5 21.99 4.0 25.13 4.5 28.27 5.0 31.42 5.5 34.56 6.0 37.70
[编辑] 参见
管理格式标志 (公共成员函数) | |
清除特定的格式标志 (公共成员函数) |