c++ string double 互转

来源:互联网 发布:画板软件哪个好 编辑:程序博客网 时间:2024/05/17 06:06
string str = "123456"
double   dblValue   =   _atof(str);


C++通过ostringstream实现任意类型转string

2010年9月23日代码疯子

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
 int a = 55;
 double b = 65.123;
 string str = "";

 //头文件是sstream
 ostringstream oss;
 oss << a << "---" << b;

 str = oss.str();
 cout << str << endl;
 return 0;
}
输出就是55—65.123,怎么样,转换起来非常的自由。就和输出到屏幕一样。


Copyed From 程序人生 
Home Page:http://www.programlife.net 
Source URL:http://www.programlife.net/cpp-ostringstream-covert-to-string.html