C_str
函数原型 const E * c_str() const
该函数返回 const char* 指针
可以将一个string类型的字符串转换成为常规c语言中的字符串
eg:
string s = "hello world";
const char* str = s.c_str();
cout<< str <<endl;
char *s1 = "hello world";
cout << s1 << endl;
一个问题
下面代码返回什么?
分析: a 和 b的值虽然相同,但是a.c_str()==b.c_str()比较的是存储字符串位置的地址, 因为c_str()函数返回的是指针 ,同时a和b是两个不同的对象,内部数据存储的位置也不相同,因此不相等。所以程序返回false。
string a="hello world";
string b=a;
if (a.c_str()==b.c_str())
{
cout<<"true"<<endl;
}
else
cout<<"false"<<endl;