See More

/*--------------------- ×÷Õߣºdongfanglanyi À´Ô´£ºCSDN Ô­ÎÄ£ºhttps://blog.csdn.net/dongfanglanyi/article/details/82355982 °æÈ¨ÉùÃ÷£º±¾ÎÄΪ²©Ö÷Ô­´´ÎÄÕ£¬×ªÔØÇ븽Éϲ©ÎÄÁ´½Ó£¡ */ #include #include #include using namespace std; int main(int argc, char *argv[]) { string str = "hello world!"; char *buff; const char * cstr = str.data(); const char *cstr2 = str.c_str(); //string::nposÊÇ»úÆ÷ÉÏ×î´óµÄÕýÕûÊý£¬³£ÓÃÀ´±íʾstringµÄ½áÊøÎ»Öà size_t length = str.copy(buff,string::npos,0); buff[length] = '\0'; for(int i =0; str[i]!='\0'; i++) cout << str[i]; cout <<"data():" << cstr << endl; cout << "c_str():" << cstr2 << endl; cout << "copy():" << buff << endl; str = "chinese"; cout <<"data():" << cstr << endl; cout << "c_str():" << cstr2 << endl; cout << "copy():" << buff << endl; return 0; }