STOD不符合的boost ::区域设置正常工作 [英] stod does not work correctly with boost::locale

查看:273
本文介绍了STOD不符合的boost ::区域设置正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用boost ::区域设置和德语语言环境,其中一个逗号是小数点分隔符标准:: STOD在一起。考虑这个code:

I am trying to use boost::locale and std::stod together in a german locale where a comma is the decimal separator. Consider this code:

boost::locale::generator gen;

std::locale loc("");  // (1)
//std::locale  loc = gen("");  // (2)

std::locale::global(loc);
std::cout.imbue(loc);

std::string s = "1,1";  //float string in german locale!
double d1 = std::stod(s);
std::cout << "d1: " << d1 << std::endl;

double d2 = 2.2;
std::cout << "d2: " << d2 << std::endl;

的std ::区域LOC()创建正确的区域设置,输出是

std::locale loc("") creates the correct locale and the output is

d1: 1,1
d2: 2,2

如我所料。当我注释掉线(1)并取消线(2),输出为

as I expect. When I comment out line (1) and uncomment line (2), the output is

d1: 1
d2: 2.2

有d2的结果是可以预期的。据我了解的boost ::区域设置要我明确指定D2应该被格式化为一个数字,做

The result for d2 is to be expected. As far as I understand boost::locale wants me to explicitly specify that d2 should be formated as a number and doing

std::cout << "d2: " << boost::locale::as::number << d2 << std::endl;

重新修复了输出到2,2。问题是,性病:: STOD不考虑1,1-作为有效的浮点数再和其截断为1

fixes the output to 2,2 again. The problem is that std::stod does not consider 1,1 as a valid floating point number anymore and truncates it to 1.

我的问题是:
为什么标准:: STOD停止时,我生成我用的boost ::区域设置区域的工作?

My question is: why does std::stod stops working when I generate my locale with boost::locale ?

更多信息:我使用VC ++ 2015年,升压1.60,没有ICU时,Windows 10

Additional information: I am using VC++2015, Boost 1.60, no ICU, Windows 10

更新:

我注意到,当我设置全局区域设置两次问题是固定的,先用的std ::区域设置(),然后用升压:

I noticed that the problem is fixed when I set the global locale twice, first with std::locale("") and then with boost:

std::locale::global(std::locale(""));
bl::generator gen;
std::locale::global(gen(""));

我不知道为什么它的行为这样一来,虽然!

I have no idea why it behaves this way, though!

推荐答案

的std ::区域是很多调试一种微妙的东西和负责任的!

std::locale is a subtle thing and responsible for a lot of debugging!

让我们先从C ++类的std ::区域设置:

Let's start with the c++ class std::locale:

  std::locale loc("de_DE.utf8");  
  std::cout<<loc.name()<<"\n\n\n";

创建了德语语言环境(如果可用你的机器上,否则它会抛出),这将导致 de_DE.utf8 在控制台上。

但它不会改变的全球 C ++语言环境的对象,这是在你的程序的启动创建,是经典(C)。 的std ::区域的不带参数的构造函数返回全局状态的副本:

However it does not change the global c++ locale object, which is created at the start-up of your program and is classical ("C"). The constructor of std::locale without arguments returns a copy of the global state:

...
  std::locale loc2;
  std::cout<<loc2.name()<<"\n\n\n";

现在你应该看到 C 如果没有之前搞砸了你的语言环境。性病::区域设置()会做一些魔术和找出用户的preferences并返回为目标,改变全局状态。

Now you should see C if nothing messed up your locale before. std::locale("") would do some magic and find out the preferences of the user and return it as object, without changing the global state.

您可以更改本地状态的std ::当地::全球

  std::locale::global(loc);
  std::locale loc3;
  std::cout<<loc3.name()<<"\n\n\n";

在控制台上默认的构造结果这次在 de_DE.utf8
我们可以通过调用恢复全局状态,以经典的:

The default constructor results this time in de_DE.utf8 on the console. We can restore the global state to the classical by calling:

  std::locale::global(std::locale::classic());
  std::locale loc4;
  std::cout<<loc4.name()<<"\n\n\n";

这应该给你 C 试。

现在,创建的std ::法院时,其克隆区域从全局C ++的状态(在这里我们用stringstreams做到这一点,但它是相同的)。全局状态的变化后不影响流:

Now, when the std::cout is created it clones its locale from the global c++ state (here we do it with the stringstreams, but it the same). Later changes of the global state does not affect the stream:

 //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与1.1 - 首先是经典的第二个德国

Gives you 1.1 vs. 1,1 - the first is the classical the second german

您可以用灌输改变流的本地语言环境的对象(的std ::区域::经典())不用说,这不改变全局状态:

You can change the local locale-object of a stream with imbue(std::locale::classic()) it goes without saying, that this doesn't change the global state:

  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:

Now we are coming to std::stod. As you can imagine it uses the global c++ locale (not entirely true, bear with me) state and not the (private) state of the cout-stream:

std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";

为您提供 1对1.1 ,因为全局状态仍是de_DE.utf8,所以第一解析停止在但当地国家的std :: COUT 还是 C。恢复全局状态后,我们得到的经典行为:

gives you 1 vs. 1.1 because the global state is still "de_DE.utf8", so the first parsing stops at '.' but the local state of std::cout is still "C". After restoring the global state we get the classical behaviour:

  std::locale::global(std::locale::classic());
  std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";

现在德国1,1不能正常解析: 1.1与1

Now the German "1,1" is not parsed properly: 1.1 vs. 1

现在你可能会认为,我们都做了,但还有更多 - 我答应给大家介绍一下的std :: STOD

Now you might think we are done, but there is more - I promised to tell you about std::stod.

接下来向全球C ++语言环境有所谓的(全球)C语言环境(来自于C语言,而不是与经典的C语言环境相混淆)。每次我们改变了全球C ++语言环境的C语言环境时已经变过。

Next to the global c++ locale there is so called (global) C locale (comes from the C language and not to be confused with the classical "C" locale). Each time we changed the global c++ locale the C locale has been changed too.

获取/ C语言环境的设置可以与做的std ::的setlocale(...)。要查询当前值运行:

Getting/setting of the C locale can be done with std::setlocale(...). To query the current value run:

std::cout<<"(global) C locale is "<<std::setlocale(LC_ALL,NULL)<<"\n";

(全局)C语言环境是C 。要设置C语言环境中运行:

to see (global) C locale is C.To set the C locale run:

  assert(std::setlocale(LC_ALL,"de_DE.utf8")!=NULL);
  std::cout<<"(global) C locale is "<<std::setlocale(LC_ALL,NULL)<<"\n";

这将产生(全球)C语言环境是de_DE.utf8 。但是,什么是现在的全球C ++语言环境?

which yields (global) C locale is de_DE.utf8. But what is now the global c++ locale?

std::cout<<"global c++ state: "<<std::locale().name()<<"\n";

正如你可能期望,C一无所知C ++全局区域设置并使其保持不变:全球C ++状态:C

现在我们不在堪萨斯州了!旧的C函数会使用C语言环境和新的C ++函数全局C ++。勇敢去面对滑稽调试!

Now we are not in Kansas any more! The old c-functions would use the C-locale and new c++ function the global c++. Brace yourself for funny debugging!

你所期望的。

std::cout<<"C: "<<std::stod("1.1")<<" vs. DE :"<<std::stod("1,1")<<"\n";

办? 的std :: STOD 是一个全新的C ++ 11毕竟功能,它应该使用全局C ++语言环境!再想一想......

to do? std::stod is a brand-new c++11 function after all and it should use global c++ locale! Think again...:

1 vs. 1.1

它得到了德国的格式正确,因为C语言环境设置为de_DE.utf8,它的引擎盖下使用旧的C风格的函数。

It gets the German format right, because the C-locale is set to 'de_DE.utf8' and it uses old C-style functions under the hood.

只是为了完整起见,在的std ::流使用全局C ++语言环境:

Just for the sake of completeness, the std::streams use the global c++ locale:

  std::stringstream stream;//creating with global c++ locale
  stream<<1.1;
  std::cout<<"I'm still in 'C' format: "<<stream.str()<<"\n";

为您提供:我仍然在C格式为:1.1

编辑:的另一种方法来解析字符串,不与全局区域设置搞乱或者被它干扰:

An alternative method to parse string without messing with global locale or be disturbed by it:

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
}

下面的测试显示:

The following tests shows:

  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";

这只有第一和最后的转换是成功的:

That only the first and the last conversions are successful:

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的可能不是最快的,但有它的优点...

std::stringstream may be not the fastest but has its merits...

这篇关于STOD不符合的boost ::区域设置正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆