数字到字符串、字符串到数字的转换
来源:互联网 发布:php对象的作用 编辑:程序博客网 时间:2024/05/20 17:02
数字到字符串与字符串到数字的转换在很多情况下用到。然而C++98就没有提供这样一个工具,但是我们可以通过很多方式来实现这种转换。在后来的C++发展中也收录了这一简单常用的模块。我们来看看其发展历程。
C++ -------stringstreams
C++的stream库是很强大的,以前的时候通过
的输入/输出转换成字符串。使用stringstreams的时候需要 #include <sstream>。
我们来看下面的例子:
数字到字符串
通过stringstreams将数字转换到字符串,需要两个步骤:
1.将变量的数值输出到流中;
2.获取流中的字符串类容。
int value = 1234;string str;stringstreams conent;conent<<value;str = conent.str();
这段代码可以简化一下:
int value = 1234;
string str = static_cast<ostringstream *>(&(ostringstream ()<<value))->str();
这里构造了一个字符串流对象,执行输出流操作ostringstream ()<<value,然后<<返回一个ostream(ostringstream 的基类)的引用。这个结果需要转换称字符串流,所以我们用static_cast进行强制转换。
我们来看看完整的例子:
#include<iostream>#include <sstream>#include <string> // this should be already included in <sstream>using std::ostringstream;using std::cout;using std::endl;using std::string;int main(void){double Number = 0.12; // Number to convert to stringstring str = static_cast<ostringstream *>(&(ostringstream()<<Number))->str();cout<<str<<endl;}输出:
0.12
字符串到数字的转换。
通过stringstreams将一个字符串转换为数字也需要两个步骤:
1.利用字符串来构造流;
2.将值读入变量。
这个转换中我们会用到istringstream
。这将字符串往数字转换的时候我们需要注意些问题,例如这个字符串为“abcd”,是不能转换的,所以在转换的时候需要做一些安全检查。
下面一个简单的例子:
string str="1234";int value;istringstream content(str);if(!(content>>value))//转换失败的时候将返回0 此处进行安全检查 value = 0;我们的程序可以写得更简单些:
string str = "1234";
int value;
if(!istringstream(str)>>value))value = 0;
istringstream通过str构造一个对象,然后通过>>value转换成一个数值变量。如果转换失败,value被设置为0。
通常istringstream
(可以被用于输入输出)被用于更复杂的环境中。
我们可以将上面的两个例子做成简单的函数:
template <typename T> string NumberToString ( T value) { ostringstream ss; ss << value; return ss.str(); }用法:NumberToString(value),返回字符串。
template <typename T> T StringToNumber ( const string &str) { istringstream ss(str); T result; return ss >> result ? result : 0; }用法:StringToNumber<数据类型>(str),返回数值。
第二个函数中,我们没有进行安全检查。
C++11
在c++11中引入的标准库的一些函数,我们可以利用一些转换函数进行轻松的转换。std::to_string 将数值转换成字符串。
std::stoi std::stof 前者将字符串转换成int,后者将字符串转换成float。
这些函数声明在<string>中。
int value= 123;string str= to_string(value);str= "456"value= stoi(str);当然,对于标准库,我们的态度应该是能用标准库中的函数尽量用。
C++ boost library
lexical_cast
进行直接转换。str = boost::lexical_cast<string>(value);// String to number conversion:value = boost::lexical_cast<Type>(str);//Type为我们想转换为的数据类型上述转换并没用考虑到异常情况。当boost::lexical_cast转换失败时,它会抛出boost::bad_lexical_cast异常,这个异常继承于std::bad_cast.
try{ value = boost::lexical_cast<Type>(Text);}catch ( const boost::bad_lexical_cast &exc ) // conversion failed, exception thrown by lexical_cast and caught{ value = 0; }
C --- stdio
int value = 123; char Result[16]; sprintf ( Result, "%d", value);
字符串到数字char Text[] = "456"; int Result;sscanf ( Text, "%d", &Result );如果字符串本身不是个数字,将导致转换失败。这个时候我们应该返回0。
char Text[] = "456"; int Result; int Succeeded = sscanf ( Text, "%d", &Result );if ( !Succeeded || Succeeded == EOF ) Result = 0;
- 字符串到数字的转换
- 数字到字符串、字符串到数字的转换
- num_rmb 数字到字符串的转换
- 实现字符串到数字的转换
- 对象到字符串、数字的转换
- 数字1到字符串0001的转换
- java 数字到字符串的转换
- 从字符串转换到数字。
- 数字到字符串转换问题
- oracle转换数字到格式化字符串
- C/C++字符串转换到数字的函数
- 使用stringstream实现数字到字符串之间的转换
- C/C++字符串转换到数字的函数
- C/C++字符串转换到数字的函数
- 华为机试——字符串到数字的转换
- 字符串16进制数字转换到Ascll字符串
- _tcstol 字符串到数字的转化
- _tcstol 字符串到数字的转化
- zzuli 1432 背包again(求最小不能被得到的价值
- STL中vector删除一个元素
- POJ 3561
- 对Java中多态的认识总结
- Leetcode Longest Substring Without Repeating Characters
- 数字到字符串、字符串到数字的转换
- 阶乘
- Java中尽量使用StringBuilder和StringBuffer进行字符串连接
- java环境变量
- 王达大讲堂视频课程会员优惠购买方案
- 在云计算中实现安全、可伸缩、细粒度数据的访问控制
- 核心卷I笔记 集合
- SEO学习笔记二(SEO实战密码读书笔记)
- java中Class.getResource用法