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调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa