加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

转:C++ string的万能转换,从long string 之间的转换来看看

(2016-09-08 20:21:29)

string 转 long 

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


 

[plain] view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. using namespace std;  
  5. int main ()  
  6.  
  7.     string "1234567890";  
  8.     long 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


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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有