1. 程式人生 > >《C++面向物件程式設計》課程筆記 lessen8 string 類

《C++面向物件程式設計》課程筆記 lessen8 string 類

1. string 類

1 string 物件的初始化

  • string 類是模板類:
typedef basic_string <char> string; 
  •  使用 string 類要包含標頭檔案 <string>
  • string 物件的初始化:
string s1("MARCH");
string s2 = "APRIL";
string s3(8,'x');
  •  錯誤的初始化方法:(不能通過一個字元和一個整數來初始化 sstring)
string error1 = 'c';
string error2('u');
string error3 = 22;
string error4(8);
  •  可以將字元賦值給 string 物件。但不能用字元初始化 string 物件。
string s;
s = 'n';
  • string 物件的長度用成員函式 length() 讀取 
string s("hello");
cout << s.length() << endl;
  •  string 支援流讀取運算子
string stringObject;
cin >> stringObject;
  •  string 支援 getline 函式
string s;
getline(cin , s);

 2 string 物件的賦值和連線

  • 用 “=” 賦值 
string s1("cat");
string s2;
s2 = s1;
  •  用 assign 成員函式複製
string s1("cat");
string s3;
s3.assign(s1);
  •  用 assign 成員函式部分複製
string s1("catpig");
string s3;
s3.assign(s1,1,3);
//從 s1 中下標為1的字元開始複製3個字元
  • 單個字元複製 
s2[5] = s1[3] = 'a';
  • 逐個訪問 string 物件中的字元
string s1("Hello");
for(int i=0;i<s1.length();i++)
	cout << s1.at(i) << endl;

 成員函式 at 會做範圍檢查,如果超出範圍,會丟擲 out_of_range 異常,而下標運算子 [ ] 不做範圍檢查。

  • 用 “+” 運算子連線字串 
string s1("good");
string s2("morning!");
s1 += s2;
cout << s1;
  •  用成員函式 append 連線字串
string s1("good");
string s2("morning!");
s1.append(s2);
cout << s1;
s2.append(s1,3,s1.size()); //s1.stze(),s1字元數
//下標為3開始,s1,size()個字元,如果字串內沒有足夠字元,則複製到字串最後一個字元
cout << s2;

 3 比較 string 物件

  • 用關係運算符比較 string 大小:==,>,>=,<,<=,!= 。返回值都是 bool 型別,成立返回 true,否則返回 false。
  • 用compare 成員函式比較 string 物件的大小。
string s1("hello"),s2("hello"),s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3);//s1:1-2; s3:0-3
int f5 = s1.compare(0,s1.size(),s3); //s1:0-end
cout << f1 << endl << f2 << endl << f3 << endl << f4 << endl << f5 << endl;

//輸出:0 // hello == hello
//      1 // hello > hell
//	   -1 // hell  < hello
//	   -1 // el < hell
//   	1 // hello > hell

4 string 物件子串

  • 成員函式 substr
string s1("hello world");
string s2;
s2 = s1.substr(4,5); //下標為4開始的5個字元
cout << s2 <<endl;

 5 交換 string 物件

  • 成員函式 swap
string s1("hello world");
string s2("really");
s1.swap(s2);
cout << s1 <<endl;
cout << s2 <<endl;

 6 尋找 string 物件中的字元

  •  成員函式 find()
string s1("hello world");
s1.find("lo");

在 s1 中從前向後查詢 “lo” 第一次出現的地方,如果找到,返回 “lo” 開始的位置,即 “l” 所在的位置下標。如果找不到,返回 string::npos ( string 中定義的靜態常量)

  • 成員函式 rfind()
string s1("hello world");
s1.rfind("lo");

在 s1 中從後向前查詢 “lo” 第一次出現的地方,如果找到,返回 “lo” 開始的位置,即 “l” 所在的位置下標。如果找不到,返回 string::npos ( string 中定義的靜態常量)

  • 成員函式 find() 還可以指定查詢開始的位置
string s1("hello worlld");
cout << s1.find("ll",1) << endl;
cout << s1.find("ll",2) << endl;
cout << s1.find("ll",3) << endl;
//分別從下標1,2,3開始查詢 "ll"。
  •  成員函式 find_first_of()  (尋找 string 中的字元)
string s1("hello world");
s1.find_first_of("abcd");

在 s1 中從前向後查詢 “abcd” 中任何一個字元第一次出現的地方,如果找到,返回找到字母的位置。如果找不到,返回 string::npos。

  • 成員函式 find_last_of()  (尋找 string 中的字元)
string s1("hello world");
s1.find_last_of("abcd");

 在 s1 中查詢 “abcd” 中任何一個字元最後一次出現的地方,如果找到,返回找到字母的位置。如果找不到,返回 string::npos。

  •  成員函式 find_first_not_of()  (尋找 string 中的第一個不在 “abcd” 中的字元)
  •  成員函式 find_last_not_of()  (尋找 string 中的最後一個不在 “abcd” 中的字元)

7 刪除 string 物件中的字元

  • 成員函式 erase() 
string s1("hello world");
s1.erase(5); //去掉下標5及之後的字元
  •  成員函式 replace()
string s1("hello world");
s1.replace(2,3."haha"); //將s1中下標2開始的3個字元換成 "haha"

 8 在 string 物件中插入字元

  • 成員函式 insert() 
string s1("hello world");
string s2("show insert");
s1.insert(5,s2);//將s2插入s1下標5的位置
cout << s1 << endl;
s1.insert(2,s2,5,3);
//將s2中下標5開始的3個字元插入s1下標2的位置
cout << s1 << endl;

9 轉換成C語言式 char * 字串

  • 成員函式 c_str()
string s1("hello world");
printf("%s\n",s1.c_str());
//s1.c_str()返回傳統的 const char * 型別字串,且該字串以 '\0' 結尾