/* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> //#include "MySqrt.h" #include <math.h> #include <vector> #include <typeinfo> #include <exception> #include <stdexcept> #include<string.h> #include<sstream> #include<stdio.h> using namespace std; class Base{ public: Base(){ cout<<"create the base"<<endl; } virtual ~Base(){ cout<<"destroy the base"<<endl; } }; class Derived: public Base{ public: Derived(){ cout<<"derived is created"<<endl; } virtual ~Derived(){ cout<<"Derived is destroying"<<endl; } }; //double -->string string doubleConverToString(double d){ ostringstream os; if(os << d) return os.str(); return "invalid conversion"; } //string-->double double stringConverTodouble(string str){ istringstream iss(str); double x; if(iss >> x) return x; return 0.0; } //c-function double-->string string cfunctionDtoS(double d){ char str[100]; sprintf(str,"%.3lf",d); return str; } //c-function string->double double cfunctionStoD(string str){ double dd; sscanf(str.c_str(),"%lf",&dd); return dd; } int main() { //string-->char* string str("string"); char *p = const_cast<char *>(str.c_str()); cout<<"string->char*"<<p<<endl; //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 double dd = 3.14; string ddstr = doubleConverToString(dd); cout<<ddstr<<endl; //string--->double&float string strp = "3.5555555555"; double strdd = stringConverTodouble(strp); cout<<strdd<<endl; cout<<atof(strp.c_str())<<endl; //c-function double->string string ss = cfunctionDtoS(3.146789); cout<<"ss"<<ss<<endl; //c-function string->string double cdd = cfunctionStoD("3.14259"); cout<<cdd<<endl; }
运行结果
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
时间: 2024-10-25 15:55:28