String转换成整型,c_str()

代码
int main() {
string s1 = “v”;
string s2 = “vt”;
int a = *s1.c_str();
int b = *s2.c_str();
cout << a << ” ” << b << endl;
system(“pause”);
}

输出的结果
输出的a=b
原因:c_str()返回的是一个临时指针,c_str()只能转换成const char *,因为这两个字符串的首个字符一样,所以返回的结果也一样

猜你喜欢

转载自blog.csdn.net/weixin_43081805/article/details/82684799