字符串流实现类型转换

利用字符串流类可实现字符串转换为其他类型,采用字符串流类可以很容易的把各种类型的字符串(不管是c类型还是string类型)转换为单精度,双精度。使用字符串流类,需要包含头文件#include,对于istringstream提供读字符串的功能,ostringstream提供写字符串的功能,stringstream提供了读写字符串的功能。

示例代码如下:

#include

#include

#include

 

using namespace std;

 

int main()

{

    string test="123.456";

    stringstream ss;

    double a;

    ss<<test;

    ss>>a;

    cout<<"字符串:"<<test<<endl;

    cout<<"双精度类型:"<<a+100<<endl;

    return 0;

}

运行结果:

 

字符串流实现类型转换

posted @ 2016-04-30 16:16  硫酸亚铜  阅读(276)  评论(0编辑  收藏  举报