namespace std {
class locale;
}
概要
localeは、地域化のデータを表現するクラスである。localeはファセットの集合を保持しており、has_facet()とuse_facet()の関数テンプレートでlocaleが保持しているファセットを調査・取得できる。ファセットはfacetの派生クラスである。
メンバ関数
| 名前 | 説明 |
|---|---|
(constructor) |
コンストラクタ |
(destructor) |
デストラクタ |
operator= |
代入演算子 |
combine |
合成 |
name |
名前の取得 |
encoding |
エンコーディングの取得 (C++26) |
operator== |
等値比較 |
operator!= |
非等値比較 |
operator() |
照合オブジェクトを使用した文字列比較 |
静的メンバ関数
| 名前 | 説明 |
|---|---|
global |
グローバルロケールの設定 |
classic |
Cロケールを表すオブジェクトの取得 |
メンバ型
| 名前 | 説明 |
|---|---|
category |
ビットマスク型 int。 localeが保持するファセットを識別する用途。 |
id |
facetの識別用のクラス |
facet |
ファセットの基底クラス |
メンバ定数
| 名前 | 説明 |
|---|---|
static const category none = 0; |
ファセットなし |
static const category collate = 0x10; |
照合ファセット |
static const category ctype = 0x20; |
文字分類ファセット |
static const category monetary = 0x40; |
金額ファセット |
static const category numeric = 0x80; |
数値ファセット |
static const category time = 0x100; |
日時ファセット |
static const category messages = 0x200; |
メッセージファセット |
static const category all = collate | ctype | monetary | numeric | time | messages; |
全てのファセット |
例
基本的な使い方
#include <iostream>
#include <locale>
int main()
{
// 現在の環境のロケールを取得する
std::locale loc("");
// ロケールに基いて文字を大文字化する
std::cout << std::toupper('a', loc) << std::endl;
// ロケールに基いて数値を出力する
std::cout.imbue(loc);
std::cout << 1234567 << std::endl;
}
出力例
A
1,234,567
Cロケールを使う
処理系標準のロケール(Cロケール)は、classic()で取得できる。このロケールでは、数値の桁区切りなどの地域化は行われない。
出力
1234567
ロケール名を指定する
ロケール名を文字列で指定して、特定の地域のロケールを構築できる。以下は日本語ロケールを指定する例である。
指定できるロケール名は処理系や環境に依存する。
出力例
1,234,567
日時を出力する
ストリームにロケールを設定すると、std::put_timeによる日時の出力もそのロケールに従う。
#include <ctime>
#include <iomanip>
#include <iostream>
#include <locale>
int main()
{
// 日本語ロケールを設定する
std::locale loc("ja_JP.UTF-8");
std::cout.imbue(loc);
// 2025年8月19日 (火) 12:34:56
std::tm tm{};
tm.tm_year = 2025 - 1900;
tm.tm_mon = 8 - 1;
tm.tm_mday = 19;
tm.tm_hour = 12;
tm.tm_min = 34;
tm.tm_sec = 56;
tm.tm_wday = 2;
// ロケールに基いて日時を出力する(%a はロケールに応じた曜日名)
std::cout << std::put_time(&tm, "%Y年%m月%d日 (%a) %H時%M分%S秒") << std::endl;
}
出力例
2025年08月19日 (火) 12時34分56秒
バージョン
言語
- C++98