1. string -> int :
1)atoi() C语言库
std::string numStr = "123";
int number = std::atoi( numStr.c_str() );
string s = "17";
stringstream ss;
ss<<s;
int i;
ss>>i;
cout<<i<<endl; // 17
(3)stringstream可以吞下任何类型,根据实际需要吐出不同的类型。
string s = "17";
int i = boost::lexical_cast<int>(s);
cout<<i<<endl; // 17
常用:atoi() 类似的,转成float atof() 转成long atol() 实际上是char类型向数值类型的转换
2.int -> string
(1)sprintf() c语言库,可格式化字符串
int a = 10;
char s[10];
sprintf(s,"%d",a);
string str = s;
(2)stringstream
int n = 65535;
stringstream ss;
string s;
ss << n;
ss >> s;
3. char* <-> string
char* -> string,隐式转换
char* c = "s1fd2";
string s = c;
string -> char*,直接调string 的方法
string s = "sdf231";
char* c = s.c_str();