Boost库中的lexical_cast函数功能很强大,但存在一个浮点数的精度问题。在阿炯的《走进Boost》中描述如下:
#include
#include
#include
int main()
{
using std::string;
const double d = 123.1234567;
string s = boost::lexical_case(d);
std::cout< return 0;
}
以上程序预想的结果是得到:"123.1234567",但是实际上我们只会得到"123.123",因为默认情况下std::stringstream的精度是6。这算是boost::lexical_cast的一个bug。
阿炯提供一个解决办法:
对头文件作如下修改:
//....
templatesource>
Target lexical_cast(Source arg) {
//.....
Target result;
//以上一行是增加内容
interpreter.precision(std::numeric_limits::digits10);
if(!(interpreter<>result)||!(interpreter>>std::ws).eof())
//.........
}
我按照上述方法在我的机器上怎么也得不到正确结果。(BCB 6)
就算可行的话,也存在以下问题:只能得到一种精度的字符串。
我使用以下方法解决以上的问题:
利用C++中的函数重载机制,增加一个函数:
template
Target lexical_cast(Source arg)
{
//......
Target result;
if(!(interpreter << arg) || !(interpreter >> result) ||
!(interpreter >> std::ws).eof())
//....
}
template
// 下面一行修改了
Target lexical_cast(Source arg,int length)
{
# ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM
std::strstream interpreter; // for out-of-the-box g++ 2.95.2
# else
std::stringstream interpreter;
# endif
Target result;
// 增加下面一行
interpreter.precision( length );
if(!(interpreter << arg) || !(interpreter >> result) ||
!(interpreter >> std::ws).eof())
throw bad_lexical_cast();
return result;
}
}
使用时如下:
std::string s = boost::lexical_cast<:string>(123.1234567,10);
即可以得到正确的结果,而且原来的函数不变。
小结
不管多好的东西,总会有些小问题和不如意的东西,能够自己动手加以改进,是很好的事。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992543/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752043/viewspace-992543/