名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | locale‎ | numpunct
 
 
 
 
 
定義於標頭檔案 <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 ' ';  } // 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

[編輯] 參閱

提供用作千位分隔符的字元
(虛保護成員函式) [編輯]