名前空間
変種
操作

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) public メンバ関数。 最も派生したクラスのメンバ関数 do_grouping を呼びます。
2) num_put::put() (および basic_ostream::operator<<) によって書式化された数値出力の各グループの桁数をそれぞれの char の要素に保持する std::string を返します。

グループはバイナリ値として格納されます。 3桁のグループは '\3'、51桁のグループは '3' です。 返された文字列のインデックスゼロの文字は最も右のグループの桁数を保持します。 インデックス1の文字は右から2つめのグループの桁数を保持します。 返された文字列の最後の文字によって示されるグループは数値の (左の部分の) 残りのすべての桁のグループ化のために再使用されます。

[編集] 戻り値

グループを保持する 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';
}

出力:

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

[編集] 関連項目

桁区切りとして使用される文字を提供します
(仮想プロテクテッドメンバ関数) [edit]