1.string to others
C++封装了一系列函数,使得string类型的数据能够被方便地转换到其他数据类型。
这些函数名有着统一的风格,即stox(string to x,x为要转换到的类型名缩写)。
| 函数名 | 转换到 |
| stoi | int |
| stol | long |
| stoul | unsigned long |
| stoll | long long |
| stoull | unsigned long long |
| stof | float |
| stod | double |
| stold | long double |
- 示例:
1 string s; 2 s = "1989"; 3 int num = stoi(s); 4 long long nums = stol(s); 5 cout << num + 30 << endl; 6 cout << nums + 2147481659 << endl;//比int的最大值大1
运行结果:

2.others to string
从其他类型转换到string只要记住一个函数即可。
涉及到宽字符时有所变化。
| 函数名 | 转换为 |
| to_string | string |
| to_wstring | wstring |
示例:
1 string s; 2 wstring ss; 3 int num = 1989; 4 long long max_sz = ss.max_size(); 5 s = to_string(num); 6 cout << s[0] << s[1] << s[2] << s[3] << endl; 7 cout << max_sz << endl; 8 //实在找不到这么长的演示...
运行结果:

以后遇到麻烦的字符串和大数终于可以轻松一点了。