一、结论
使用C++11,并且导入头文件 #include<string>
other ---> string | to_string (int / long / float /...) | |
string --->other | stoi | string 转 int |
stol | string 转long | |
stoll | string 转 long long | |
stoul | string 转 unsigned long | |
stoull | string 转 unsigned long long | |
stof | string 转 float | |
stod | string 转 double |
注:在日常使用中, 最常用的是stoll和stod这两个函数, stoll可以兼容stoi,stol; 而stod可以兼容stof。
二、举例
1、stoll()
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "11111";
int a = stoll(s);
cout << a;
return 0; }
输出: 11111
2、stod()
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "11.11";
double a = stod(s);
cout << a;
return 0; }
输出:11.11
三、参考资料
[【最贴心】C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)_来老铁干了这碗代码的博客-CSDN博客_stoul](https://blog.csdn.net/weixin_43899069/article/details/110290292?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-4&spm=1001.2101.3001.4242)