Demo_1:
#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")是同样的效果.
2,std::locale("C")这个表达式根据一个给定名称产生一个locale对象,"C"是一个特殊的名称,事实上他是所有C++实现唯一必须支持额名称,C++标准库并未强制要求支持其他locale比如utf8之类的.
Demo_2:
#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::locale
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,拷贝构造函数.
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对象都将是改变后的 执行字符集.