string s = "hello";
os
將s寫到輸入流os當中,返回os
cout << s << endl;
<從is中讀取字串並賦給s,返回is
cin >> s; //從鍵盤輸入
getline(is, s)
從is中讀取一行賦給s,返回is
getline(cin,s)
empty()
檢查s時候為空string,返回真或假
cout << s.empty() << endl;
>>>0
size()
返回s的中字元的數量
cout << s.size() << endl;
>>>5
返回s中第n個字元的引用
cout << s[2] << endl;
>>>l
+
把符號兩端字串連在一起
string s1 = " world";
cout << s+s1 << endl;
>>>hello world;
=
把符號右端字串賦值給符號左端的字串
string s1 = "hi"
s = s1;
cout << s <>>hi
s1==s2 和 s1!=s2
判斷s1和s2兩個字串是否相等或者不等,返回真或假
<, <=, >, >=
判斷符號兩邊字串的大小(對應位置字元相同,長則大;對應位置字元不同,大小由第一組相異字元決定)
學習筆記 C String類
string類位於命名空間std中,使用時需要宣告標頭檔案和所屬命名空間 include using namespace std 在未輸入之前,string物件的長度被自動設定為0 使用以下方法獲取string物件的輸入 char charr 20 int length strlen charr c...
C String類的實現
參考c primer.string類的實現,清翔兔 06,jan.include using namespace std class string string void private char m data inline string string const char str if str m...
C String類的實現
include using namespace std class string string void private char m data inline string string const char str inline string string const string other i...