我如何使用标准的C ++ 11功能从char16_t字符串(char16_t *)中读取双精度字?
我尝试过:
#include <iostream>
#include <sstream>
#include <string>
double char16_to_double( const char16_t* s )
{
double n = 0;
std::basic_stringstream<char16_t> ss( s );
ss >> n;
return n;
}
int main()
{
try {
double n = char16_to_double( u"0.1" );
std::cout << "Value of n: " << n << std::endl;
} catch( ... ) {
std::cout << "Exception!" << std::endl;
}
return 0;
}
但是,当我使用g ++-7进行编译时,它将在https://cpp.sh中引发异常,或者导致0而不是0.1。正确的方法是什么?
[我发现的一种可能的解决方案是通过std::wstring_convert
将char16_t(采用UTF16编码)转换为char字符串:
您可以只转换**const char16_t*** to const char*
并使用]中的atof()>
所以您的函数将如下所示: