C++ 字串
std::string 是 C++ 的字串型別:用 + 拼接,size() 求長度,s[i] 取字元,substr 擷取,find 查詢,找不到時返回 std::string::npos。
名字、位址、商品編號……文字在程式裡到處都是。C++ 用 std::string 放一串文字(要 #include <string>)。兩個字串用 + 就能接起來:
1#include <iostream>2#include <string>3 4int main() {5 std::string city = "上海";6 std::string street = "南京路";7 std::string address = city + street + " 100 號";8 std::cout << address << '\n';9 address += ",3 樓";10 std::cout << address << '\n';11 return 0;12}
上海南京路 100 號 上海南京路 100 號,3 樓
+ 把幾段文字連成一個新字串;+= 把一段文字接到原來那個的後面,和第 4 課裡數字的 += 一個意思。
要記住:+ 只管把文字接起來。字串裡寫的就算是數字,也不會被加起來。
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string a = "12";6 std::string b = "3";7 std::cout << a + b << '\n';8 return 0;9}
a 和 b 是字串,不是數。字串的 + 是把文字接起來:"12" 後面接上 "3",就是 "123"。拼出全名
✓用 + 把 last 和 first 接起來,顯示 林小明,你好!。
林小明,你好!
1#include <iostream>2#include <string>3 4int main() {5 std::string first = "小明";6 std::string last = "林";7 8 return 0;9}
長度 size()
s.size() 告訴你字串有多長。.size() 寫在變數後面,是字串自己帶的成員函式,以後還會見到很多這樣的寫法。
1#include <iostream>2#include <string>3 4int main() {5 std::string code = "AB-1024";6 std::string name = "牛奶";7 std::cout << code.size() << '\n';8 std::cout << name.size() << '\n';9 return 0;10}
7 6
AB-1024 是 7 個字元,顯示 7。可是「牛奶」兩個字顯示的是 6:size() 數的是位元組,英文字母、數字、符號各佔 1 個位元組,一個漢字在這裡佔 3 個。所以這一課裡按位置取字元的例子,都只用英文字母和數字。
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string s = "C++ 2026";6 std::cout << s.size() << '\n';7 return 0;8}
C、+、+、空格、2、0、2、6,一共 8 個。英文字母、數字和符號各佔 1 個位元組。用下標取字元
字串裡的每個字元都有一個編號,叫下標,從 0 開始。s[0] 是第一個字元,s[s.size() - 1] 是最後一個。取出來的是 char。std::string 是可以改的:給 s[i] 賦值,就換掉了那個字元。
1#include <iostream>2#include <string>3 4int main() {5 std::string name = "jack";6 std::cout << name[0] << '\n';7 std::cout << name[name.size() - 1] << '\n';8 name[0] = 'J';9 std::cout << name << '\n';10 return 0;11}
j k Jack
點單步,看 name 從 jack 變成 Jack。注意 'J' 是單引號:name[0] 是一個字元,放進去的也得是一個字元。
s[1] → 'e'
拖動 i:下標從 0 開始數
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string w = "cat";6 w[0] = 'b';7 std::cout << w << " " << w[2] << '\n';8 return 0;9}
w[0] = 'b' 把第一個字元 c 換成了 b,w 變成 bat。w[2] 是第三個字元 t:下標從 0 開始數。下標越界
"AB12" 只有 4 個字元,下標是 0 到 3。用 s[10] 去取一個不存在的字元,是未定義行為:普通的電腦上可能顯示一個亂七八糟的字元,也可能出別的事。我們的引擎會停在這一行:
1#include <iostream>2#include <string>3 4int main() {5 std::string code = "AB12";6 std::cout << code[3] << '\n';7 std::cout << code[10] << '\n';8 return 0;9}
2
index out of range發生了什麼: 停在第 7 行:你訪問了 string[10],可它只有 4 個字元(下標 0~3)。
code[3] 是最後一個字元 2,沒問題;code[10] 越界了。(C++ 倒是允許讀 code[4]:它是字串結尾一個看不見的空字元,對你沒什麼用。)用下標之前,先想一想它是不是小於 size()。
std::string s = "hello"; 哪個寫法取出的是最後一個字元 o?
s[4],也可以寫成 s[s.size() - 1]。s[5] 就是 s[s.size()],那是結尾看不見的空字元,不是 o。substr 和 find
s.substr(start, n) 從下標 start 開始取 n 個字元,組成一個新字串;不寫 n 就一直取到最後。s.find("x") 找 x 第一次出現的下標。兩個配起來,就能把一個郵箱拆成兩半:
1#include <iostream>2#include <string>3 4int main() {5 std::string email = "[email protected]";6 auto at = email.find("@");7 std::cout << at << '\n';8 std::cout << email.substr(0, at) << '\n';9 std::cout << email.substr(at + 1) << '\n';10 return 0;11}
3 lin example.com
@ 在下標 3。substr(0, 3) 是前 3 個字元 lin,substr(4) 是從下標 4 到最後。find 返回的型別名字很長,這裡正好用 auto。
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string s = "banana";6 std::cout << s.find("an") << " " << s.substr(2, 3) << '\n';7 return 0;8}
"an" 第一次出現在下標 1(b 是 0)。substr(2, 3) 從下標 2 開始取 3 個字元:n、a、n。郵箱裡的使用者名稱
✓用 find 和 substr 取出 @ 前面的部分,顯示 使用者名稱:zhang.wei。
使用者名稱:zhang.wei
1#include <iostream>2#include <string>3 4int main() {5 std::string email = "[email protected]";6 7 return 0;8}
找不到:npos
s.find("x") 找到了,就返回下標。要是字串裡根本沒有 x 呢?找不到的時候,find 回傳一個特別的值 std::string::npos(「沒有這個位置」):
1#include <iostream>2#include <string>3 4int main() {5 std::string email = "lin.example.com";6 auto at = email.find("@");7 std::cout << (at == std::string::npos) << '\n';8 return 0;9}
1
顯示 1,表示「是真的」:沒找到。== 這一句外面要加括號,不然 << 會先算。第 6 課學了 if,就能寫「找到了怎樣,找不到怎樣」。
字串 s 裡沒有字母 z。s.find("z") 返回什麼?
std::string::npos,專門表示「沒有這個位置」。不會是 0:0 是第一個字元的下標,返回 0 的意思是在開頭就找到了。比較字串
== 看兩個字串是不是一模一樣,< 和 > 按字典的順序比,從第一個字元開始一個一個比:
1#include <iostream>2#include <string>3 4int main() {5 std::string a = "apple";6 std::string b = "banana";7 std::cout << (a == "apple") << '\n';8 std::cout << (a < b) << '\n';9 std::cout << ("Zoo" < a) << '\n';10 return 0;11}
1 1 1
三行都是 1。最後一行可能讓你意外:比的是字元的編碼,所有大寫字母都排在小寫字母前面,所以 Zoo 比 apple 小。漢字也是按編碼比,不是按拼音或筆畫。
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string a = "Cat";6 std::string b = "cat";7 std::cout << (a == b) << " " << (a < b) << '\n';8 return 0;9}
a == b 是 0。從第一個字元比起:大寫 C 的編碼比小寫 c 小,所以 a < b 是 1。數字和字串互換
要把數接進字串,先用 std::to_string 把它變成字串;反過來,std::stoi 把一串數字變成 int(i 是 int 的意思):
1#include <iostream>2#include <string>3 4int main() {5 int count = 3;6 std::string msg = "購物車裡有 " + std::to_string(count) + " 件商品";7 std::cout << msg << '\n';8 std::string text = "42";9 int n = std::stoi(text);10 std::cout << n + 8 << '\n';11 return 0;12}
購物車裡有 3 件商品 50
text + "8" 會是 "428";std::stoi(text) + 8 才是 50。從輸入讀到的、從檔案裡拿到的數字常常是字串,算之前要先轉換。
這段程式顯示什麼?
1#include <iostream>2#include <string>3 4int main() {5 std::string a = "7";6 int b = std::stoi(a);7 std::cout << a + "1" << " " << b + 1 << '\n';8 return 0;9}
a 是字串,a + "1" 把文字接起來,得到 71。b 是 std::stoi 變出來的 int 7,b + 1 才是 8。修好它:文字加數字
✓這段程式編譯不過。改好它,讓它顯示 共 5 件。
共 5 件
1#include <iostream>2#include <string>3 4int main() {5 int count = 5;6 std::string msg = "共 " + count + " 件";7 std::cout << msg << '\n';8 return 0;9}
這一課學會了
std::string放一串文字;+把字串接起來,+=接到後面。s.size()是位元組數:英文字元各佔 1 個,漢字在這裡各佔 3 個。s[i]取出下標i的字元,下標從 0 開始;字串可以改:s[0] = 'J';。- 下標越界(
i不小於size())是未定義行為,引擎會停下。 s.substr(start, n)取一段;s.find("x")找位置,找不到是std::string::npos。==、<比較字串,按字元的編碼一個一個比,大寫字母排在小寫前面。std::to_string(n)把數變成字串,std::stoi(s)把字串變成int。