c++ 中double与string之间的转换,char *
运行代码为
/*
* 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 *的更多相关文章
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- java中int和String之间的转换
String 转为int int i = Integer.parseInt([String]); int i = Integer.valueOf(my_str).intValue(); int转为St ...
- C++中数字与字符串之间的转换 scanf string总结(复习必读)
1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- C++中数字与字符串之间的转换,别人的,
C++中数字与字符串之间的转换 1.字符串数字之间的转换 (1)string --> char * string str("OK"); char * p = st ...
- C++中数字与字符串之间的转换(转)
http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> char ...
- Java学习--Java 中基本类型和字符串之间的转换
Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...
- 如何在Byte[]和String之间进行转换
源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...
- Java 中基本类型和字符串之间的转换
Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...
随机推荐
- 【强力卸载】使用Uninstall Tool卸载各类不易卸载的软件
Uninstall Tool 经测试卸载MySql5.7.18成功. 下载地址: http://files.cnblogs.com/files/xiaohi/%E3%80%90%E8%BD%AF%E4 ...
- 页面中插入视频兼容ie8以上的浏览器
有时候页面中需要插入视频,如果不考虑ie8的话:就是直接用h5标签<video></video>就可以了: 但是有的时候需求是需要考虑ie8,这样我们就可以用插件,这种vide ...
- HDU4417 线段树 + 离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 , 线段树(或树状数组) + 离线处理 最近看了几道线段树的题都是需要离线处理数据的,正好这块比 ...
- xtarbackup恢复
xbstream -x < ynhw-mysql-slave.01.mysql.prod.sg_fullbak_20180326134255.xbstream -C /data/mysql cd ...
- bzoj1189 [HNOI2007]紧急疏散
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...
- 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)
点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...
- Java环境变量搭建(Windows环境)
变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91 // 要根据自己的实际路径配置 变量名:CLASSPATH 变量值:. ...
- 【转】浅谈Node.js单线程模型
Node.js采用 事件驱动 和 异步I/O 的方式,实现了一个单线程.高并发的运行时环境,而单线程就意味着同一时间只能做一件事,那么Node.js如何利用单线程来实现高并发和异步I/O?本文将围绕这 ...
- Incorrect key file for table './xx_db/xx_table.MYI'; try to repair it
解决办法: 可以先运行 CHECK TABLE 表名 检查下是否存在错误. 然后运行 REPAIR TABLE 表名 进行修复.
- nginx 如何配置来获取用户真实IP