各种基本类型与string的转换--基于C++11标准/C++11标准之前

基于C++11标准

如果你用的编译器是基于最新的C++11标准,那么这个问题就变很简单,因为<string>已经封装好了对应的转换方法:

标准库中定义:

  • to_string(val);将其它类型转换为string。
  • stoi(s,p,b),将s转化成int
  • stol(s,p,b),将s转化成long
  • stod(s,p,b),将s转化成double
  • stoi(s,p,b), 返回s的起始子串(表示整数内容的字符串)的数值,返回数值为int
  • stol(s,p,b), 返回s的起始子串(表示整数内容的字符串)的数值,返回数值为long
  • stoul(s,p,b),返回s的起始子串(表示整数内容的字符串)的数值,返回数值为unsigned long
  • stoll(s,p,b),返回s的起始子串(表示整数内容的字符串)的数值,返回数值为long long
  • stoull(s,p,b),返回s的起始子串(表示整数内容的字符串)的数值,返回数值为unsigned long long
  • stof(s, p), 返回s的起始子串(表示浮点数内容)的数值,返回值的类型是float
  • stod(s, p), 返回s的起始子串(表示浮点数内容)的数值,返回值的类型double
  • stold(s, p); 返回s的起始子串(表示浮点数内容)的数值,返回值的类型long double

其中b表示转换所用的基数,默认为10(表示十进制).p是size_t的指针,用来保存s中第一个非数值字符的下标,p默认为0,即函数不返回下标.



VS2013下编译通过

[cpp]  view plain  copy
  1. void testTypeConvert()  
  2. {  
  3.     //int --> string  
  4.     int i = 5;  
  5.     string s = to_string(i);  
  6.     cout << s << endl;  
  7.     //double --> string  
  8.     double d = 3.14;  
  9.     cout << to_string(d) << endl;  
  10.     //long --> string  
  11.     long l = 123234567;  
  12.     cout << to_string(l) << endl;  
  13.     //char --> string  
  14.     char c = 'a';  
  15.     cout << to_string(c) << endl;   //自动转换成int类型的参数  
  16.     //char --> string  
  17.     string cStr; cStr += c;  
  18.     cout << cStr << endl;  
  19.   
  20.   
  21.     s = "123.257";  
  22.     //string --> int;  
  23.     cout << stoi(s) << endl;  
  24.     //string --> int  
  25.     cout << stol(s) << endl;  
  26.     //string --> float  
  27.     cout << stof(s) << endl;  
  28.     //string --> doubel  
  29.     cout << stod(s) << endl;  
  30. }  

结果如下:

5

3.140000

123234567

97

a

 

123

123

123.257

123.257

 

C++11标准之前

C++11标准之前没有提供相应的方法可以调用,就得自己写转换方法了,代码如下:

从其它类型转换为string,定义一个模板类的方法。

string转换为其它类型,定义多个重载函数。

 

VS2010下编译通过

[cpp]  view plain  copy
  1. #include <strstream>  
  2. template<class T>  
  3. string convertToString(const T val)  
  4. {  
  5.     string s;  
  6.     std::strstream ss;  
  7.     ss << val;  
  8.     ss >> s;  
  9.     return s;  
  10. }  
  11.   
  12.   
  13. int convertStringToInt(const string &s)  
  14. {  
  15.     int val;  
  16.     std::strstream ss;  
  17.     ss << s;  
  18.     ss >> val;  
  19.     return val;  
  20. }  
  21.   
  22. double convertStringToDouble(const string &s)  
  23. {  
  24.     double val;  
  25.     std::strstream ss;  
  26.     ss << s;  
  27.     ss >> val;  
  28.     return val;  
  29. }  
  30.   
  31. long convertStringToLong(const string &s)  
  32. {  
  33.     long val;  
  34.     std::strstream ss;  
  35.     ss << s;  
  36.     ss >> val;  
  37.     return val;  
  38. }  
  39.   
  40. void testConvert()  
  41. {  
  42.     //convert other type to string  
  43.     cout << "convert other type to string:" << endl;  
  44.     string s = convertToString(44.5);  
  45.     cout << s << endl;  
  46.     int ii = 125;  
  47.     cout << convertToString(ii) << endl;  
  48.     double dd = 3.1415926;  
  49.     cout << convertToString(dd) << endl;  
  50.   
  51.     //convert from string to other type  
  52.     cout << "convert from string to other type:" << endl;  
  53.     int i = convertStringToInt("12.5");  
  54.     cout << i << endl;  
  55.     double d = convertStringToDouble("12.5");  
  56.     cout << d << endl;  
  57.     long l = convertStringToLong("1234567");  
  58.     cout << l << endl;  
  59. }  

结果如下: 

convert other type to string:

44.5

125

3.14159

convert from string to other type:

12

12.5

1234567



猜你喜欢

转载自blog.csdn.net/summer00072/article/details/80753572