使用std :: stringstream将boost :: int64_t转换为字符串
Using std::stringstream to convert boost::int64_t large number to string.
请参阅以下代码,调试和Show Convert在iPhone模拟器和设备(4s)上都成功,但我想知道它是如何工作的吗?请参阅http://www.cplusplus.com/reference/iostream/ostream/operator<</,boost :: int64_t没有Overload函数。
如果我使用此功能将任何任意boost :: int64_t类型转换?提前致谢。
std::stringstream mySS;
boost::int64_t large = 4294967296889977;
mySS<<large;
std::string str = mySS.str();
其起作用的原因是boost:int64_t
确实是内置类型的类型(通常是cstdint
中定义的std::int64_t
或类似的类型),因此它可能最终与long long
相同(或类似,取决于平台)。当然,stringstream::operator<<
超载。
有关确切的定义,最好见boost/cstdint.hpp
(1.51版)。
假设这通常可以在所有主要平台上使用,这可能是一个相对安全的选择。但是我怀疑任何人都可以为此提供保证。
如果您使用std::stringstream
的目的是在整数和字符串之间转换,那么您可以做的最安全的事情就是使用Boost自己的转换方式:boost::lexical_cast
(1.51版本)。这是完成的方式:
#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/lexical_cast.hpp>
int main()
{
boost::int64_t i = 12;
std::string s = boost::lexical_cast<std::string>(i);
std::cout << s << std::endl;
return 0;
}
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数