string转char
使用方法string.c_str()
注意只能转换为常字符指针
string s;
const char* c;
s = "153";
c = s.c_str();
string转int
使用函数stoi(string)
string s;
int i;
s = "153";
i = stoi(s);
string 转double、long、float
stol(string), stof(string), stod(string)
实例
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
const char* c;
int i;
double d;
s = "153";
c = s.c_str();
i = stoi(s);
d = i;
cout<<s<<endl<<c<<endl<<i<<endl<<d<<endl;
s = c;
cout<<s<<endl;
return 0;
}