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

相关文章

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

C++迭代器失效的避坑指南

《C++迭代器失效的避坑指南》在C++中,迭代器(iterator)是一种类似指针的对象,用于遍历STL容器(如vector、list、map等),迭代器失效是指在对容器进行某些操作后... 目录1. 什么是迭代器失效?2. 哪些操作会导致迭代器失效?2.1 vector 的插入操作(push_back,

exfat和ntfs哪个好? U盘格式化选择NTFS与exFAT的详细区别对比

《exfat和ntfs哪个好?U盘格式化选择NTFS与exFAT的详细区别对比》exFAT和NTFS是两种常见的文件系统,它们各自具有独特的优势和适用场景,以下是关于exFAT和NTFS的详细对比... 无论你是刚入手了内置 SSD 还是便携式移动硬盘或 U 盘,都需要先将它格式化成电脑或设备能够识别的「文

idea报错java: 非法字符: ‘\ufeff‘的解决步骤以及说明

《idea报错java:非法字符:‘ufeff‘的解决步骤以及说明》:本文主要介绍idea报错java:非法字符:ufeff的解决步骤以及说明,文章详细解释了为什么在Java中会出现uf... 目录BOM是什么?1. BOM的作用2. 为什么会出现 \ufeff 错误?3. 如何解决 \ufeff 问题?最

使用Java编写一个字符脱敏工具类

《使用Java编写一个字符脱敏工具类》这篇文章主要为大家详细介绍了如何使用Java编写一个字符脱敏工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、字符脱敏工具类2、测试工具类3、测试结果1、字符脱敏工具类import lombok.extern.slf4j.Slf4j

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav