std::numpunct<CharT>::grouping, std::numpunct<CharT>::do_grouping
来自 cppreference.cn
定义于头文件 <locale> |
||
public: std::string grouping() const; |
(1) | |
protected: virtual std::string do_grouping() const; |
(2) | |
1) 公有成员函数,调用派生程度最高的类的成员函数
do_grouping
。此函数返回一个字符串 vec,它被用作整数值的向量。(例如,"\003" 指定每组 3 位数字,而 "3" 表示每组 51 位数字。)每个元素 vec[i] 表示数字整数部分中第 i
个数字组的位数,从右边开始计数:vec[0] 存放最右边组的位数,vec[1] 存放从右边数第二个组的位数,等等。由最后一个字符 vec[vec.size()-1] 指示的分组会重复使用,以对数字(左侧部分)中所有剩余的数字进行分组。如果 vec[i] 为非正数或等于 CHAR_MAX,则相应数字组的大小是无限的。
[编辑] 返回值
类型为 std::string 的对象,包含分组信息。std::numpunct
的标准特化返回空字符串,表示不分组。典型的分组(例如 en_US
区域设置)返回 "\003"。
[编辑] 示例
运行此代码
#include <iostream> #include <limits> #include <locale> struct space_out : std::numpunct<char> { char do_thousands_sep() const { return ' '; } // separate with spaces std::string do_grouping() const { return "\1"; } // groups of 1 digit }; struct g123 : std::numpunct<char> { std::string do_grouping() const { return "\1\2\3"; } }; int main() { std::cout << "Default locale: " << 12345678 << '\n'; std::cout.imbue(std::locale(std::cout.getloc(), new space_out)); std::cout << "Locale with modified numpunct: " << 12345678 << '\n'; std::cout.imbue(std::locale(std::cout.getloc(), new g123)); std::cout << "Locale with \\1\\2\\3 grouping: " << std::numeric_limits<unsigned long long>::max() << '\n' << "Same, for a floating-point number: " << std::fixed << 123456789.123456789 << '\n'; }
输出
Default locale: 12345678 Locale with modified numpunct: 1 2 3 4 5 6 7 8 Locale with \1\2\3 grouping: 18,446,744,073,709,551,61,5 Same, for a floating-point number: 123,456,78,9.123457
[编辑] 参阅
[virtual] |
提供用作千位分隔符的字符 (虚保护成员函数) |