Namespaces
Variants

std::numpunct<CharT>:: grouping, std::numpunct<CharT>:: do_grouping

From cppreference.net
定义于头文件 <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 ,则对应数字组的大小不受限制。

返回值

类型为 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 ' ';  } // 用空格分隔
    std::string do_grouping() const { return "\1"; } // 每1位数字一组
};
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

参见

提供用作千位分隔符的字符
(虚受保护成员函数)