低版本C++ string的萬能轉換,從long 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流其實本身就是流,一個原理的,不同調用方法。



粗制濫造,歡迎斧正


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM