c++11 标准模板(STL)本地化库 - 平面类别(std::time_put) - 格式化数值为字符序列以输出

本文主要是介绍c++11 标准模板(STL)本地化库 - 平面类别(std::time_put) - 格式化数值为字符序列以输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

格式化数值为字符序列以输出

std::time_put
template<

    class CharT,
    class OutputIt = std::ostreambuf_iterator<CharT>

> class num_put;

std::num_put 封装格式化数值为字符串的规则。具体而言支持 bool 、 long 、 unsigned long 、 long long 、 unsigned long long 、 double 、 long double 、 void* 类型和所有能隐式转换到它们的类型(例如 int 或 float )。标准格式化输出运算符(如 cout << n; )用 I/O 流的 locale 的 std::num_put 平面生成数字的文本表示。

继承图

类型要求

- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。

特化

标准库提供二个孤立(独立于本地环境)的全特化和二个部分特化:

定义于头文件 <locale>

std::num_put<char>创建数的窄字符串表示
std::num_put<wchar_t>创建数的宽字符串表示
std::num_put<char, OutputIt>用定制输出迭代器创建数的窄字符串表示
std::num_put<wchar_t, OutputIt>用定制输出迭代器创建数的宽字符串表示

另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。

成员类型

成员类型定义
char_typeCharT
iter_typeOutputIt

成员函数

(构造函数)

构造新的 num_put 平面
(公开成员函数)

(析构函数)

销毁 num_put 平面
(受保护成员函数)

put

调用 do_put
(公开成员函数)

受保护成员函数

do_put

[虚]

格式化数字并写入到输出流
(虚受保护成员函数)

成员对象

static std::locale::id id

locale 的 id
(公开成员对象)

构造新的 num_put 平面

std::num_put<CharT,OutputIt>::num_put

explicit num_put( std::size_t refs = 0 );

创建 std::num_put 平面并转发引用计数 refs 到基类构造函数 locale::facet::facet() 。

参数

refs-开始的引用计数

销毁 num_put 平面

std::num_put<CharT,OutputIt>::~num_put

protected: ~num_put();

析构 std::num_put 平面。此析构函数为受保护且为虚(由于基类析构函数为虚)。 std::num_put 类型对象,同大多数平面,只能在最后一个实装此平面的 std::locale 离开作用域时,或若用户定义导出自 std::num_put 并实现公开构造函数,才会被销毁。

调用示例

 

#include <iostream>
#include <locale>struct Destructible_num_put : public std::num_put<wchar_t>
{Destructible_num_put(std::size_t refs = 0) : num_put(refs) {}// 注意:隐式析构函数为公开
};int main()
{Destructible_num_put dc;// std::num_put<wchar_t> c;  // 编译错误:受保护析构函数return 0;
}

调用 do_put & 格式化数字并写入到输出流

std::num_put<CharT,OutputIt>::put, std::num_put<CharT,OutputIt>::do_put

public:
iter_type put( iter_type out, std::ios_base& str, char_type fill, bool v ) const;

(1)

iter_type put( iter_type out, std::ios_base& str, char_type fill, long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, long long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, double v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, long double v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, const void* v ) const;

protected:
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, bool v ) const;

(2)

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long long v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long long ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, double v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long double v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, const void* v ) const;

1) 公开成员函数,调用最终导出类的受保护虚成员函数 do_put

2) 写字符到输出序列 out ,这些字符表示 v 的值,按格式化标志 str.flags() 和流 str 中感染的 locale 的 std::numpunct 和 std::ctype 平面所要求者格式化。此函数为所有有格式输出流运算符,如 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);

  • v 的类型为 bool
    • 若 boolalpha == 0 ,则转换 v 为 int 类型并进行整数输出。
    • 若 boolalpha != 0 ,则若 v == true 则获得std::use_facet<std::numpunct<charT>>(str.getloc()).truename() ,或若 v == false 则获得 std::use_facet<std::numpunct<charT>>(str.getloc()).falsename() ,并以 *out++ = c 输出该字符串的每个相继字符 cout 。此情况下不进行进一步的操作,函数返回 out
  • v 的类型为整数类型,则选择下列首个可应用选项:

若 basefield == oct ,则将使用转换指定符 %o

若 basefield == hex && !uppercase ,则将使用转换指定符 %x

若 basefield == hex ,则将使用转换指定符 %X

v 的类型有符号,则将使用转换指定符 %d

v 的类型无符号,则将使用转换指定符 %u

  • 对于整数类型,若需要则添加长度修饰符到转换指定:对于 long 和 unsigned long 为 l ,对于 long long 和 unsigned long long 为 ll 。
  • v 为浮点类型,则选择首个可应用选项:

若 floatfield == std::ios_base::fixed ,则将使用转换指定符 %f

若 floatfield == std::ios_base::scientific && !uppercase ,则将使用转换指定符 %e

若 floatfield == std::ios_base::scientific ,则将使用转换指定符 %E

若 floatfield == (std::ios_base::fixed | std::ios_base::scientific) && !uppercase ,则将使用转换指定符 %a

若 floatfield == (std::ios_base::fixed | std::ios_base::scientific) ,则将使用转换指定符 %A

(C++11 起)

若 !uppercase ,则将使用转换指定符 %g

否则,将使用转换指定符 %G

  • v 的类型为 long double ,则添加长度修饰符 L 到转换指定符。
  • 另外若 floatfield != (ios_base::fixed | ios_base::scientific) 则 (C++11 起)添加设为 str.precision() 的精度指定符
  • 对于整数和浮点类型,若设置了 showpos ,则前附修饰符 + 。
  • 对于整数类型,若设置了 showbase ,则前附修饰符 # 。
  • 对于浮点类型,若设置了 showpoint ,则前附修饰符 # 。
  • v 的类型为 void* ,则将使用格式指定符 %p 。
  • 如同以在 "C" 本地环境中调用 std::printf(spec, v) 创建窄字符串,其中 spec 是所选的格式指定符。
阶段 2 :本地环境限定转换
  • 调用 std::use_facet<std::ctype<CharT>>(str.getloc()).widen(c) ,将阶段 1 中获得的每个异于小数点 '.' 的字符 c 转换为 CharT
  • 对于算术类型,按照 std::use_facet<std::numpunct<CharT>>(str.getloc()).grouping() 所提供的分组规则,插入从 std::use_facet<std::numpunct<CharT>>(str.getloc()).thousands_sep() 获得的千分隔字符到序列中。
  • 以 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 :输出

如同用 *out++ = c 输出来自阶段 3 的 CharT 序列的每个相继字符 c

参数

out-指向首个要写入字符的迭代器
str-取得格式化信息来源的流
fill-需要填充结果到域宽时使用的填充字符
v-转换为字符串并输出的值

返回值

out

注意

转换指定 #o (例如产生自 std::showbase 和 std::oct 的组合)不计为填充字符。

格式化浮点值为 hexfloat 时(即 floatfield == (std::ios_base::fixed | std::ios_base::scientific) 时),不使用流的精度;而是始终以足以准确表示值的精度打印该数。(C++11 起)

调用示例 windows

 

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <Windows.h>std::vector<std::wstring> locals;BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{locals.push_back(pStr);return TRUE;
}std::string stows(const std::wstring& ws)
{std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";setlocale(LC_ALL, "chs");const wchar_t* _Source = ws.c_str();size_t _Dsize = 2 * ws.size() + 1;char *_Dest = new char[_Dsize];memset(_Dest, 0, _Dsize);wcstombs(_Dest, _Source, _Dsize);std::string result = _Dest;delete[]_Dest;setlocale(LC_ALL, curLocale.c_str());return result;
}// 此定制 num_put 平面输出所有整数类型的平方(除了 long long )
struct squaring_num_put : std::num_put<char>
{iter_type do_put(iter_type s, std::ios_base& f,char_type fill, long v) const{return std::num_put<char>::do_put(s, f, fill, v * v);}iter_type do_put(iter_type s, std::ios_base& f,char_type fill, unsigned long v) const{return std::num_put<char>::do_put(s, f, fill, v * v);}
};int main()
{EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);for (std::vector<std::wstring>::const_iterator str = locals.begin();str != locals.end(); ++str){std::locale::global(std::locale(stows(*str)));std::cout.imbue(std::locale(stows(*str)));std::cout << "The locale " << std::cout.getloc().name() << ' ';auto& facet = std::use_facet<std::num_put<char>>(std::cout.getloc());facet.put(std::cout, std::cout, '0', 2.71);std::cout << "          ";std::cout.imbue(std::locale(std::cout.getloc(), new squaring_num_put));std::cout << 6 << "     " << -12 << std::endl;}return 0;
}

输出

The locale de-DE_phoneb 2,71          36     144
The locale es-ES_tradnl 2,71          36     144
The locale hu-HU_technl 2,71          36     144
The locale ja-JP_radstr 2.71          36     144
The locale ka-GE_modern 2,71          36     144
The locale x-IV_mathan 2.71          36     144
The locale zh-CN_phoneb 2.71          36     144
The locale zh-CN_stroke 2.71          36     144
The locale zh-HK_radstr 2.71          36     144
The locale zh-MO_radstr 2.71          36     144
The locale zh-MO_stroke 2.71          36     144
The locale zh-SG_phoneb 2.71          36     144
The locale zh-SG_stroke 2.71          36     144
The locale zh-TW_pronun 2.71          36     144
The locale zh-TW_radstr 2.71          36     144

这篇关于c++11 标准模板(STL)本地化库 - 平面类别(std::time_put) - 格式化数值为字符序列以输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966981

相关文章

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee