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. 使 ...
随机推荐
- hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)
思路: 完全看题目中的介绍就行了.还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦.比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存.步骤就是 ...
- Android(java)学习笔记93:为什么局部内部类只能访问外部类中的 final型的常量
为什么匿名内部类参数必须为final类型: 1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变 ...
- systemd 中的requires, wants, before, after
man systemd.unit man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...
- Java代码工具箱之解析单行单列简单Excel
1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...
- 第十一篇、UITableView headerview下拉放大
核心代码: -(void)createTableViewHeaderView{ _tableViewHeaderView = [[UIView alloc] initWithFrame:(CGRect ...
- 散列表的ASL计算
题目: 已知关键字序列为{30,25,72,38,8,17,59},设散列表表长为15.散列函数是H(key)=key MOD 13,处理冲突的方法为二次探测法Hi= ( H(key) + di )m ...
- 如何使用koa实现socket.io官网的例子
socket.io官网中使用express实现了一个最简单的IM即时聊天,今天我们使用koa来实现一下 ### 框架准备 确保你本地已经安装好了nodejs和npm,使用koa要求node版本> ...
- 二 python并发编程之多进程-理论
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- Xadmin后台管理系统搭建基于Django1.11.11+Python3.6
安装python及Django百度即可 主要介绍Xadmin安装 访问地址:https://github.com/sshwsfc/xadmin 下载 安装好之后,将xamdin目录复制到项目 我放在 ...
- Visual Studio-IIS Express 支持局域网访问配置
转自:http://www.itnose.net/detail/6132793.html 注意:本人测试后,发现个问题,不知是我个人的VS问题还是普遍的.就是将配置文件中的新增的节点注释后,会导致页面 ...