看板 EE_DSnP 關於我們 聯絡資訊
from老師Topic4的第23張投影片(如下) 不知道最後那4行 在我們教了一些pointer後 能否請高手詳細解釋一下呢? 謝謝! char str1[] = “Hello”; string str2 = “World”; cout << str1 << endl; // Hello cout << str2 << endl; // World cout << str2.c_str() << endl; cout << str1[2] << endl; // l cout << str2[2] << endl; // r cout << str1.length() << endl; // Error!! cout << strlen(str1) << endl; // 5 cout << str2.length() << endl; // 5 // FYI... char* str3 = str1; const char* str4 = str2.c_str(); cout << str3 << endl; // Hello cout << str4 << endl; // World -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.240.71
ric2k1:strl 的型態是 char *const, 就是儲存陣列開頭的記憶體位址 11/01 01:16
ric2k1:c_str() 是 class string 的 member function, 是去取得 11/01 01:16
ric2k1:string object 裡面的 const char* 11/01 01:17
ric2k1:注意: char* const 與 const char* 是不一樣的, 11/01 01:17
ric2k1:我們很快就會講到. 11/01 01:18