C++中各种基本数据类型转换为string类型

string 转 long 

那必须是万年atoi(),不过得配合c_str()使用!


[plain]  view plain  copy
  1. #include <string>  
  2. #include <iostream>  
  3. #include <stdlib.h>  
  4. using namespace std;  
  5. int main ()  
  6. {  
  7.     string a = "1234567890";  
  8.     long b = atoi(a.c_str());  
  9.     cout<<b<<endl;  
  10.     return 0;  
  11. }  


注意:atoi()在 stdlib.h


但是,这不是今天的重点!!!更加变态的方法,用String stream

[cpp]  view plain  copy
  1. long stol(string str)  
  2. {  
  3.     long result;  
  4.     istringstream is(str);  
  5.     is >> result;  
  6.     return result;  
  7. }  


long 转 string 


[cpp]  view plain  copy
  1. string ltos(long l)  
  2. {  
  3.     ostringstream os;  
  4.     os<<l;  
  5.     string result;  
  6.     istringstream is(os.str());  
  7.     is>>result;  
  8.     return result;  
  9.   
  10. }  




太变态的string流


测试测试所有的基础类型转换


string 转 int

[cpp]  view plain  copy
  1. int stoi(string str)  
  2. {  
  3.     int result;  
  4.     istringstream is(str);  
  5.     is >> result;  
  6.     return result;  
  7. }  

通过!

string 转float 

[cpp]  view plain  copy
  1. float stof(string str)  
  2. {  
  3.     float result;  
  4.     istringstream is(str);  
  5.     is >> result;  
  6.     return result;  
  7. }  

通过!

string 转double

[plain]  view plain  copy
  1. double stod(string str)  
  2. {  
  3.     double result;  
  4.     istringstream is(str);  
  5.     is >> result;  
  6.     return result;  
  7. }  

通过!


int 转 string

[cpp]  view plain  copy
  1. string itos(int i)  
  2. {  
  3.     ostringstream os;  
  4.     os<<i;  
  5.     string result;  
  6.     istringstream is(os.str());  
  7.     is>>result;  
  8.     return result;  
  9.   
  10. }  

通过!

float 转 string

[cpp]  view plain  copy
  1. string ftos(float f)  
  2. {  
  3.     ostringstream os;  
  4.     os<<f;  
  5.     string result;  
  6.     istringstream is(os.str());  
  7.     is>>result;  
  8.     return result;  
  9.   
  10. }  

通过!

double 转 string

[cpp]  view plain  copy
  1. string dtos(double d)  
  2. {  
  3.     ostringstream os;  
  4.     os<<d;  
  5.     string result;  
  6.     istringstream is(os.str());  
  7.     is>>result;  
  8.     return result;  
  9.   
  10. }  

通过!


* 转string

[cpp]  view plain  copy
  1. string *tos(* i)     //改一下函数名,改一下类型,搞定  
  2. {  
  3.     ostringstream os;  
  4.     os<<i;  
  5.     string result;  
  6.     istringstream is(os.str());  
  7.     is>>result;  
  8.     return result;  
  9.   
  10. }  

将*换成想要的类型就可以执行 *转string


string 转 *

[cpp]  view plain  copy
  1. * sto*(string str) //改一下函数名,变量类型,搞定  
  2. {  
  3.     * result;  
  4.     istringstream is(str);  
  5.     is >> result;  
  6.     return result;  
  7. }  
将*换成想要的类型就可以执行 string转*

也可以重载函数,达到万能函数转换




这些测试完全是自己不想写项目,偷懒写点文章安慰自己!囧~


记得包含头文件#include <sstream>


总结:使用string 流和标准io流其实本身就是流,一个原理的,不同调用方法

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值