运行代码为

 /*
* 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[];
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;
}

运行结果

 string->char*string
char * -->stringchar
3.14
3.55556
3.55556
ss3.
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[];
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

c++ 中double与string之间的转换,char *的更多相关文章

  1. java中Integer 和String 之间的转换

    java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...

  2. java中int和String之间的转换

    String 转为int int i = Integer.parseInt([String]); int i = Integer.valueOf(my_str).intValue(); int转为St ...

  3. C++中数字与字符串之间的转换 scanf string总结(复习必读)

    1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...

  4. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  5. C++中数字与字符串之间的转换,别人的,

    C++中数字与字符串之间的转换   1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = st ...

  6. C++中数字与字符串之间的转换(转)

    http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> char ...

  7. Java学习--Java 中基本类型和字符串之间的转换

    Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...

  8. 如何在Byte[]和String之间进行转换

    源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...

  9. Java 中基本类型和字符串之间的转换

    Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...

随机推荐

  1. Android(java)学习笔记94: SurfaceView使用

    1. SurfaceView简介    在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入). ...

  2. SOA体系-三大核心部件

    1.ESB(Enterprise Service Bus)企业服务总线.ESB是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素.从 ...

  3. 搜狗浏览器特性页面JS

    http://ie.sogou.com/features4.2.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN ...

  4. python换行

    python中如果一行代码太长,看着不方便时,怎么办? 只需要在需要换行的地方添加上符号 \ 就行了.

  5. SQL数据库查询出一张表中重复的数据,按某个字段来查找。

    例如表名为Course 需要查询出name重复的有那些??? 解答如下: 补充: 如:查询每个姓名出现大于2次,SQL如下 SELECT COUNT(NAME) as '出现次数',  NAME FR ...

  6. Sum All Primes-freecodecamp算法题目

    Sum All Primes 1.要求 求小于等于给定数值的质数之和. 只有 1 和它本身两个约数的数叫质数.例如,2 是质数,因为它只能被 1 和 2 整除.1 不是质数,因为它只能被自身整除. 2 ...

  7. 【图论】[USACO]控制公司 Controlling Companies

    玄妙的搜索 题目描述 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.(此处略去一句废话)据说,如果至少满足了以下三个条件之一,公司A就可以控制公司B了: 公司A = 公司B ...

  8. 简单的Maven+SpringMVC

    一.SpringMVC非注解编程 1:修改pom.xml文件(相当于非Maven项目的导入jar包) <!-- https://mvnrepository.com/artifact/org.sp ...

  9. ubuntu16.04更换镜像源

    1.备份原有 cp /etc/apt/sources.list /etc/apt/sources.list.old 2.打开阿里巴巴镜像源:  https://opsx.alibaba.com/mir ...

  10. 小技巧之padding-bottom实现等比例图片缩放

    1.padding-bottom 如果用%来表示的话,计算是根据父元素的width的值进行计算的. 例:父元素.wrapper的width是100px,height设置为0, padding-bott ...