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
。2) 返回一个 std::string,其中每个 char 元素保存由 num_put::put() (以及,因此,basic_ostream::operator<<) 格式化的数值输出中每组数字的位数。
此函数返回一个字符串 vec,它被用作整数值向量。(例如,"\003" 指定每组 3 位数字,而 "3" 意味着每组 51 位数字。)。每个元素 vec[i] 表示数字整数部分的第 i
个数字组中的位数,从右边计数:vec[0] 保存最右边组的位数,vec[1] - 第二个从右边的组,等等。最后一个字符 vec[vec.size()-1] 指示的分组被重复使用以对数字(左侧部分)中的所有剩余数字进行分组。如果 vec[i] 为非正数或等于 CHAR_MAX,则相应数字组的大小不受限制。
[edit] 返回值
类型为 std::string 的对象,持有分组。 std::numpunct
的标准特化返回一个空字符串,表示没有分组。 典型的分组(例如 en_US
区域设置)返回 "\003"。
[edit] 示例
运行此代码
#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
[edit] 参见
[虚拟] |
提供用作千位分隔符的字符 (虚拟保护成员函数) |