长话短说: boost::locale
仅更改全局 c++-locale 对象,但不更改 C-locale。 stod
使用 C-locale 而不是全局 c++-locale 对象。std::locale
更改:全局 c++ 语言环境对象和 C 语言环境。
整个故事: std::locale
是一个微妙的事情,负责很多调试!
让我们从 c++ 类 std::locale 开始:
std::locale loc("de_DE.utf8");
std::cout<<loc.name()<<"\n\n\n";
创建德语语言环境(如果它在您的机器上可用,否则会抛出),这会在de_DE.utf8
控制台上产生。
但是,它不会更改全局c++ 语言环境对象,该对象是在程序启动时创建的并且是经典的(“C”)。不带参数的构造std::locale
函数返回全局状态的副本:
...
std::locale loc2;
std::cout<<loc2.name()<<"\n\n\n";
现在你应该看看C
之前是否没有什么东西弄乱了你的语言环境。std::locale("") 会做一些魔术并找出用户的偏好并将其作为对象返回,而不会更改全局状态。
您可以使用以下命令更改本地状态std::local::global
:
std::locale::global(loc);
std::locale loc3;
std::cout<<loc3.name()<<"\n\n\n";
这次默认构造函数de_DE.utf8
在控制台上产生结果。我们可以通过调用将全局状态恢复为经典状态:
std::locale::global(std::locale::classic());
std::locale loc4;
std::cout<<loc4.name()<<"\n\n\n";
这应该再给你C
一次。
现在,当创建 std::cout 时,它会从全局 c++ 状态中克隆其语言环境(这里我们使用字符串流,但它相同)。全局状态的后续更改不会影响流:
//classical formating
std::stringstream c_stream;
//german formating:
std::locale::global(std::locale("de_DE.utf8"));
std::stringstream de_stream;
//same global locale, different results:
c_stream<<1.1;
de_stream<<1.1;
std::cout<<c_stream.str()<<" vs. "<<de_stream.str()<<"\n";
给你1.1 vs. 1,1
- 第一个是古典第二个德语
imbue(std::locale::classic())
不用说,您可以更改流的本地语言环境对象,这不会更改全局状态:
de_stream.imbue(std::locale::classic());
de_stream<<" vs. "<<1.1;
std::cout<<de_stream.str()<<"\n";
std::cout<<"global c++ state: "<<std::locale().name()<<"\n";
你会看到:
1,1 vs. 1.1
global c++ state: de_DE.utf8
现在我们来了std::stod
。正如您可以想象的那样,它使用全局 c++ 语言环境(不完全正确,请耐心等待)状态,而不是cout
-stream 的(私有)状态:
std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";
给你1 vs. 1.1
因为全局状态是静止"de_DE.utf8"
的,所以第一次解析停止在'.'
但局部状态std::cout
是静止的"C"
。恢复全局状态后,我们得到经典行为:
std::locale::global(std::locale::classic());
std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";
现在德语"1,1"
没有正确解析:1.1 vs. 1
现在你可能认为我们已经完成了,但还有更多——我答应告诉你的std::stod
。
在全局 c++ 语言环境旁边有所谓的(全局)C 语言环境(来自 C 语言,不要与经典的“C”语言环境混淆)。每次我们更改全局 c++ 语言环境时,C 语言环境也随之更改。
获取/设置 C 语言环境可以使用std::setlocale(...)
. 查询当前值运行:
std::cout<<"(global) C locale is "<<std::setlocale(LC_ALL,NULL)<<"\n";
查看(global) C locale is C
。设置 C 语言环境运行:
assert(std::setlocale(LC_ALL,"de_DE.utf8")!=NULL);
std::cout<<"(global) C locale is "<<std::setlocale(LC_ALL,NULL)<<"\n";
产生(global) C locale is de_DE.utf8
。但是现在全局 c++ 语言环境是什么?
std::cout<<"global c++ state: "<<std::locale().name()<<"\n";
如您所料,C 对 c++ 全局语言环境一无所知,并且保持不变:global c++ state: C
.
现在我们不在堪萨斯了!旧的 c 函数将使用 C 语言环境,而新的 c++ 函数将使用全局 c++。为有趣的调试做好准备!
你会期待什么
std::cout<<"C: "<<std::stod("1.1")<<" vs. DE :"<<std::stod("1,1")<<"\n";
去做?std::stod
毕竟是一个全新的 c++11 函数,它应该使用全局 c++ 语言环境!再想想...:
1 vs. 1.1
它获得了正确的德语格式,因为 C 语言环境设置为“de_DE.utf8”,并且它在后台使用旧的 C 样式函数。
只是为了完整起见,std::streams
使用全局 c++ 语言环境:
std::stringstream stream;//creating with global c++ locale
stream<<1.1;
std::cout<<"I'm still in 'C' format: "<<stream.str()<<"\n";
给你:I'm still in 'C' format: 1.1
。
编辑:另一种解析字符串而不弄乱全局语言环境或被它干扰的方法:
bool s2d(const std::string &str, double &val, const std::locale &loc=std::locale::classic()){
std::stringstream ss(str);
ss.imbue(loc);
ss>>val;
return ss.eof() && //all characters interpreted
!ss.fail(); //nothing went wrong
}
以下测试表明:
double d=0;
std::cout<<"1,1 parsed with German locale successfully :"<<s2d("1,1", d, std::locale("de_DE.utf8"))<<"\n";
std::cout<<"value retrieved: "<<d<<"\n\n";
d=0;
std::cout<<"1,1 parsed with Classical locale successfully :"<<s2d("1,1", d, std::locale::classic())<<"\n";
std::cout<<"value retrieved: "<<d<<"\n\n";
d=0;
std::cout<<"1.1 parsed with German locale successfully :"<<s2d("1.1", d, std::locale("de_DE.utf8"))<<"\n";
std::cout<<"value retrieved: "<<d<<"\n\n";
d=0;
std::cout<<"1.1 parsed with Classical locale successfully :"<<s2d("1.1", d, std::locale::classic())<<"\n";
std::cout<<"value retrieved: "<<d<<"\n\n";
只有第一次和最后一次转换成功:
1,1 parsed with German locale successfully :1
value retrieved: 1.1
1,1 parsed with Classical locale successfully :0
value retrieved: 1
1.1 parsed with German locale successfully :0
value retrieved: 11
1.1 parsed with Classical locale successfully :1
value retrieved: 1.1
std::stringstream 可能不是最快的,但有其优点......