Locale: std::locale

Demo_1:ios

#include <iostream>
#include <locale>

int main()
{
	//須要注意的是imbue()函數返回的是設置新的std::locale對象以前的std::locale.
	std::cout << std::cout.imbue(std::locale::classic()).name() << std::endl;
	
	//等價於下面:
	std::cout << std::cout.imbue(std::locale("C")).name() << std::endl;

	return 0;
}

1,其中 std::cout.imbue(std::locale::classic())是把 古典的 C locale指派給標準輸出通道.函數

所謂的 古典 C locale,其實就是採用ASCII字符集以及默認的日期格式化方式, 而沒有用其餘的locale來完成數字日期的格式化以及字符的分類工做。注意咱們經過std::locale("C")是一樣的效果.spa

2,std::locale("C")這個表達式根據一個給定名稱產生一個locale對象,"C"是一個特殊的名稱,事實上他是全部C++實現惟一必須支持額名稱,C++標準庫並未強制要求支持其餘locale好比utf8之類的.指針

 

Demo_2:code

#include <iostream>
#include <locale>

int main()
{
	//咱們設置開發環境的默認std::locale爲: 中文utf8.
	std::locale::global(std::locale("zh-CN.UTF-8"));

	//case 1: 這裏就會使用一個 "" 就會自動讀取到開發環境默認的std::locale因爲默認std::locale已經被咱們改變了
	//所以讀取到的是: 中文uft8
	std::cout << std::cin.imbue(std::locale("")).name() << std::endl;

	//case 2: 同case 1.
	std::cout << std::cin.imbue(std::locale()).name() << std::endl;

	return 0;
}

 

Head <locale>對象

std::locale::localeci

locale();
	
locale( const locale& other );
	
explicit locale( const char* std_name );

explicit locale( const std::string& std_name );
	
locale( const locale& loc_1, const char* std_name, category cat );
	
locale( const locale& loc_1, const std::string& std_name, category cat );
	
template< class Facet >
locale( const locale& other, Facet* f );
	
locale( const locale& loc_1, const locale& loc_2, category cat );

std::locale構造函數:開發

1,std::locale::locale()默認構造函數,創建一個對global C++ locale對象的拷貝.若是咱們經過std::locale::global()函數修改了global C++ locale對象那麼默認構造函數得到就是修改後的locale.字符串

2,拷貝構造函數.string

3,經過給定的字符串std_name (such as "C", or "POSIX", or "en_US.UTF-8", or "English_US.1251") ,建立一個std::locale對象.

4,經過給定的字符串(std::string)std_name (such as "C", or "POSIX", or "en_US.UTF-8", or "English_US.1251") ,建立一個std::locale對象.

5,等同於std::locale::locale(loc_1, std::locale(std_name), cat)

拷貝loc_1,其中cat(稍後會詳細介紹)分類所管轄的facet將被std::locale(std_name)中對應cat分類的facet替換掉.

6,同5.

7,拷貝other做爲,並安裝f指針指向的facet.

8,同5.

 

std::locale::operator=

const locale& operator=( const locale& other );

拷貝賦值運算符,不單單拷貝other指定的字符集的名字,其中other的facet也所有會被copy過去.

 

std::locale::combine

template< class Facet >
locale combine( const locale& other ) const;

注意這個函數須要咱們提供一個template type: loc1.combie<Facet>(loc2);

給loc1添加loc2中Facet類型的facet.

 

std::locale::name

std::string name() const;

返回程序所用的執行字符集的名字。

 

std::locale::operator()

template< class CharT, class Traits, class Alloc >

bool operator()( const basic_string<CharT,Traits,Alloc>& s1,
                 const basic_string<CharT,Traits,Alloc>& s2) const;

經過該operator()能夠判斷在當前 執行字符集下 s2和s1這兩個字符時候相等.

Demo for std::locale::operator()

#include <locale>
#include <algorithm>
#include <vector>
#include <string>
#include <cassert>
 
int main()
{
    std::vector<std::wstring> v = {L"жил", L"был", L"кот"};
    std::sort(v.begin(), v.end(), std::locale("ru_RU.UTF8"));
    assert(v[0] == L"был");
    assert(v[1] == L"жил");
    assert(v[2] == L"кот");
}

 

std::locale::classic

static const locale& classic();

返回 std::locale("C"); C++標準惟一要求必須實現支持的 執行字符集

 

std::locale::global

static locale global( const locale& loc );

使用該函數必須在任何操做以前:

改變整個程序的 執行字符集 也就是說之後調用 std::locale loc; 這種默認構造構造的std::locale對象都將是改變後的 執行字符集.

相關文章
相關標籤/搜索