C++ Primer学习笔记—— String 的数值转换
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("1.23");
string s2("p=0123+-2.34sss");
int i1 = 9;
float f1 = 8.0;
double d1 = 7.0;
string s;
int i;
float f;
double d;
s = to_string(i1);
cout << "i1 to_string: " << s << endl;
s = to_string(f1);
cout << "f1 to_string: " << s << endl;
s = to_string(d1);
cout << "d1 to_string: " << s << endl;
i = stoi(s1);
cout << "s1 stoi: " << i << endl;
f = stof(s1);
cout << "s1 stof: " << f << endl;
d = stod(s1);
cout << "s1 stod: " << d << endl;
d = stod(s2.substr(s2.find_first_of("+-.0123456789")));
cout << "s2 stoi(substr(find_first_of)): " << d << endl;
system("PAUSE");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7232f64a15d6f76b055d64088644b1bc.png#pic_center)