运行代码为
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /* 2 * main.cpp 3 * 4 * Created on: Apr 7, 2016 5 * Author: lizhen 6 */ 7 8 #include <iostream> 9 //#include "MySqrt.h" 10 #include <math.h> 11 #include <vector> 12 #include <typeinfo> 13 #include <exception> 14 #include <stdexcept> 15 #include<string.h> 16 #include<sstream> 17 #include<stdio.h> 18 19 20 using namespace std; 21 22 class Base{ 23 public: 24 Base(){ 25 cout<<"create the base"<<endl; 26 } 27 virtual ~Base(){ 28 cout<<"destroy the base"<<endl; 29 } 30 }; 31 class Derived: public Base{ 32 public: 33 Derived(){ 34 cout<<"derived is created"<<endl; 35 } 36 virtual ~Derived(){ 37 cout<<"Derived is destroying"<<endl; 38 } 39 }; 40 //double -->string 41 string doubleConverToString(double d){ 42 ostringstream os; 43 if(os << d) return os.str(); 44 return "invalid conversion"; 45 } 46 47 //string-->double 48 double stringConverTodouble(string str){ 49 istringstream iss(str); 50 51 double x; 52 if(iss >> x) return x; 53 return 0.0; 54 } 55 56 //c-function double-->string 57 string cfunctionDtoS(double d){ 58 char str[100]; 59 sprintf(str,"%.3lf",d); 60 return str; 61 } 62 //c-function string->double 63 double cfunctionStoD(string str){ 64 double dd; 65 sscanf(str.c_str(),"%lf",&dd); 66 return dd; 67 } 68 69 int main() { 70 //string-->char* 71 string str("string"); 72 char *p = const_cast<char *>(str.c_str()); 73 cout<<"string->char*"<<p<<endl; 74 75 //char* -->string 76 char *ch = const_cast<char *>("char");//warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| 77 string chstr(ch); 78 cout<<"char * -->string"<<chstr<<endl; 79 80 //double&float --->string 81 double dd = 3.14; 82 string ddstr = doubleConverToString(dd); 83 cout<<ddstr<<endl; 84 85 //string--->double&float 86 string strp = "3.5555555555"; 87 double strdd = stringConverTodouble(strp); 88 cout<<strdd<<endl; 89 cout<<atof(strp.c_str())<<endl; 90 91 92 93 //c-function double->string 94 string ss = cfunctionDtoS(3.146789); 95 cout<<"ss"<<ss<<endl; 96 97 //c-function string->string 98 double cdd = cfunctionStoD("3.14259"); 99 cout<<cdd<<endl; 100 }
运行结果
1 string->char*string 2 char * -->stringchar 3 3.14 4 3.55556 5 3.55556 6 ss3.147 7 3.14259
===========================================================
string-->char*
//string-->char* string str("string"); char *p = const_cast<char *>(str.c_str()); cout<<"string->char*"<<p<<endl;
char*-->string
//char* -->string char *ch = const_cast<char *>("char");//warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| string chstr(ch); cout<<"char * -->string"<<chstr<<endl;
===
double/float-->string
string-->double/float
利用c++中 sstream头文件中的方法
==利用 ostringstream 输出流对象,将double输出到string中
方法如下:
//double -->string string doubleConverToString(double d){ ostringstream os; if(os << d) return os.str(); return "invalid conversion"; }
==利用istringstream输入流对象,将string中的东西放到double变量中去
方法如下
//string-->double double stringConverTodouble(string str){ istringstream iss(str); double x; if(iss >> x) return x; return 0.0; }
利用标准c中的stdio.h头文件中的方法
==利用sprintf(str,"%.3lf",dd)方法,将double变量中的字符输出到字符串str中
方法如下:
//c-function double-->string string cfunctionDtoS(double d){ char str[100]; sprintf(str,"%.3lf",d); return str; }
==利用sscanf(str,"%d",&dd)方法,将字符串str中的东西,放到double变量dd中
方法如下:
//c-function string->double double cfunctionStoD(string str){ double dd; sscanf(str.c_str(),"%lf",&dd); return dd; }
其他方法
char *itoa(int value, char* string, int radix); int---->string
同样也可以将数字转字符串,不过itoa()这个函数是平台相关的(不是标准里的),故在这里不推荐使用这个函数。
另外也可以使用atoi(),atol(),atof().可以将string--->int/double/float
参考文档:
http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html