std::num_put<CharT,OutputIt>::put, std::num_put<CharT,OutputIt>::do_put
来自 cppreference.cn
定义于头文件 <locale> |
||
(1) | ||
public: iter_type put( iter_type out, std::ios_base& str, |
||
iter_type put( iter_type out, std::ios_base& str, char_type fill, long val ) const; |
||
iter_type put( iter_type out, std::ios_base& str, char_type fill, long long val ) const; |
(C++11 起) | |
iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long val ) const; |
||
iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long long val ) const; |
(C++11 起) | |
iter_type put( iter_type out, std::ios_base& str, char_type fill, double val ) const; |
||
iter_type put( iter_type out, std::ios_base& str, char_type fill, long double val ) const; |
||
iter_type put( iter_type out, std::ios_base& str, char_type fill, const void* val ) const; |
||
(2) | ||
protected: virtual iter_type do_put( iter_type out, std::ios_base& str, |
||
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long val ) const; |
||
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long long val ) const; |
(C++11 起) | |
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long val ) const; |
||
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long long val ) const; |
(C++11 起) | |
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, double val ) const; |
||
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long double val ) const; |
||
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, const void* val ) const; |
||
1) 公有成员函数,调用最派生类的受保护虚成员函数
do_put
。2) 将字符写入输出序列 out,这些字符表示 val 的值,根据格式化标志 str.flags() 以及流 str 中所注入区域设置的 std::numpunct 和 std::ctype 方面(facet)所请求的格式进行格式化。所有格式化输出流操作符都会调用此函数,例如 std::cout << n;。
转换分四个阶段进行
目录 |
[编辑] 阶段 1:转换说明符选择
- 获取 I/O 格式标志,如同通过以下方式:
- fmtflags basefield = (str.flags() & std::ios_base::basefield);
- fmtflags uppercase = (str.flags() & std::ios_base::uppercase);
- fmtflags floatfield = (str.flags() & std::ios_base::floatfield);
- fmtflags showpos = (str.flags() & std::ios_base::showpos);
- fmtflags showbase = (str.flags() & std::ios_base::showbase);
- fmtflags showpoint = (str.flags() & std::ios_base::showpoint);
- 若 val 的类型为 bool
- 若 boolalpha == 0,则将 val 转换为 int 类型并执行整数输出。
- 若 boolalpha != 0,若 val == true 则获取 std::use_facet<std::numpunct<CharT>>(str.getloc()).truename(),若 val == false 则获取 std::use_facet<std::numpunct<CharT>>(str.getloc()).falsename(),并按 *out++ = c 将该字符串的每个连续字符 c 输出到 out。在此情况下不再进行进一步处理,函数返回 out。
- 若 val 的类型为整数类型,则选择以下第一个适用的选项
- 若 basefield == oct,将使用转换说明符 %o。
- 若 basefield == hex && !uppercase,将使用转换说明符 %x。
- 若 basefield == hex,将使用转换说明符 %X。
- 若 val 的类型为有符号,将使用转换说明符 %d。
- 若 val 的类型为无符号,将使用转换说明符 %u。
- 对于整数类型,必要时会向转换说明添加长度修饰符:l 用于 long 和 unsigned long,ll 用于 long long 和 unsigned long long(C++11 起)。
- 若 val 的类型为浮点类型,则选择以下第一个适用的选项
|
(C++11 前) |
|
(C++11 起) |
- 若 floatfield == std::ios_base::scientific && !uppercase,将使用转换说明符 %e。
- 若 floatfield == std::ios_base::scientific,将使用转换说明符 %E。
|
(C++11 起) |
- 若 !uppercase,将使用转换说明符 %g。
- 否则,将使用转换说明符 %G。
- 此外
- 若 val 的类型为 long double,则将长度修饰符 L 添加到转换说明符。
- 若 val 的类型为浮点类型 且 floatfield != (ios_base::fixed | ios_base::scientific)(C++11 起),则添加精度修饰符并设置为 str.precision()。否则,不指定精度。
- 对于整数和浮点类型,若设置了 showpos,则预置修饰符 +。
- 对于整数类型,若设置了 showbase,则预置修饰符 #。
- 对于浮点类型,若设置了 showpoint,则预置修饰符 #。
- 若 val 的类型为 void*,将使用转换说明符 %p
- 将创建一个窄字符字符串,如同通过在 "C" 区域设置中调用 std::printf(spec, val),其中 spec 是所选的转换说明符。
[编辑] 阶段 2:区域设置特定转换
- 在阶段 1 中获取的每个字符 c,除了小数点 '.',都通过调用 std::use_facet<std::ctype<CharT>>(str.getloc()).widen(c) 转换为
CharT
。 - 对于算术类型,从 std::use_facet<std::numpunct<CharT>>(str.getloc()).thousands_sep() 获取的千位分隔符字符,将根据 std::use_facet<std::numpunct<CharT>>(str.getloc()).grouping() 提供的分组规则插入到序列中。
- 小数点字符 ('.') 将被 std::use_facet<std::numpunct<CharT>>(str.getloc()).decimal_point() 替换。
[编辑] 阶段 3:填充
- 调整标志通过 std::fmtflags adjustfield = (flags & (std::ios_base::adjustfield)) 获取并检查以确定填充位置,如下所示
- 若 adjustfield == std::ios_base::left,将在后面填充。
- 若 adjustfield == std::ios_base::right,将在前面填充。
- 若 adjustfield == std::ios_base::internal 且表示中出现符号字符,将在符号后填充。
- 若 adjustfield == std::ios_base::internal 且阶段 1 表示以 0x 或 0X 开头,将在 x 或 X 后填充。
- 否则,将在前面填充。
- 若 str.width() 非零(例如刚使用了 std::setw)且阶段 2 后
CharT
的数量小于 str.width(),则将 fill 字符的副本插入到填充指定的位置,以使序列长度达到 str.width()。
无论如何,都调用 str.width(0) 以取消 std::setw 的效果。
[编辑] 阶段 4:输出
阶段 3 的 CharT
序列中的每个连续字符 c 都通过 *out++ = c 输出。
[编辑] 参数
out | - | 指向要覆盖的第一个字符的迭代器 |
str | - | 从中检索格式化信息的流 |
fill | - | 当结果需要填充到字段宽度时使用的填充字符 |
val | - | 要转换为字符串并输出的值 |
[编辑] 返回值
out
[编辑] 注意
转换说明符 #o 生成的前导零(例如由 std::showbase 和 std::oct 组合产生)不计为填充字符。
当将浮点值格式化为十六进制浮点(即当 floatfield == (std::ios_base::fixed | std::ios_base::scientific))时,不使用流的精度;相反,始终以足够的精度打印数字以精确表示该值。 |
(C++11 起) |
[编辑] 示例
使用方面(facet)直接输出数字,并演示用户定义的方面
运行此代码
#include <iostream> #include <locale> // this custom num_put outputs squares of all integers (except long long) struct squaring_num_put : std::num_put<char> { iter_type do_put(iter_type out, std::ios_base& str, char_type fill, long val) const { return std::num_put<char>::do_put(out, str, fill, val * val); } iter_type do_put(iter_type out, std::ios_base& str, char_type fill, unsigned long val) const { return std::num_put<char>::do_put(out, str, fill, val * val); } }; int main() { auto& facet = std::use_facet<std::num_put<char>>(std::locale()); facet.put(std::cout, std::cout, '0', 2.71); std::cout << '\n'; std::cout.imbue(std::locale(std::cout.getloc(), new squaring_num_put)); std::cout << 6 << ' ' << -12 << '\n'; }
输出
2.71 36 144
用户定义类型的 operator<< 实现。
运行此代码
#include <iostream> #include <iterator> #include <locale> struct base { long x = 10; }; template<class CharT, class Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const base& b) { try { typename std::basic_ostream<CharT, Traits>::sentry s(os); if (s) { std::ostreambuf_iterator<CharT, Traits> it(os); std::use_facet<std::num_put<CharT>>(os.getloc()) .put(it, os, os.fill(), b.x); } } catch (...) { // set badbit on os and rethrow if required } return os; } int main() { base b; std::cout << b; }
输出
10
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 34 | C++98 | bool 重载使用了不存在的成员 truename 和 falsename 的 std::ctype |
使用这些成员 的 std::numpunct |
LWG 231 | C++98 | 精度修饰符仅在以下情况添加 (flags & fixed) != 0 或 str.precision() > 0 |
删除了这些条件 |
LWG 282 | C++98 | 千位分隔符仅在以下情况添加 阶段 2 中用于整数类型 |
也插入用于 浮点类型 |
LWG 4084 | C++11 | 无法打印 "NAN" 和 "INF" | 它们可以打印 |
[编辑] 另见
插入格式化数据 ( std::basic_ostream<CharT,Traits> 的公有成员函数) |