回目錄

附錄 H

原 Google Sites 連結:/cppzero/fu-lu-h-string

成員函數

(constructor)

constructs a basic_string (public member function)

operator=

指定字串的值 (public member function)

元素存取

operator[]

存取指定的字元 (public member function)

c_str

字串轉字元陣列 (public member function)

用法: s.c_str()

Iterators

begin

回傳開始的位置 (public member function)

end

回傳結束的位置 (public member function)

用法: s.begin()

容量

size (length)

回傳字串的大小 (public member function)

用法: s.size()

reserve

為字串預留空間 (public member function)

用法: s.reserve(數字)

如沒給數字則預設為 0

動作

insert

插入字串 (public member function)

用法: s.insert(3, “AAA”)

從 s[3] 的位置插入 “AAA” “012345”→”012AAA345”

erase

刪除字串 (public member function)

用法: s.erase(3, 2)

從 s[3]的位置開始刪掉 2 個字元 “012345”→”0125”

operator+=

在字串末端加上字串 (public member function)

replace

替換掉一段字串 (public member function)

用法: s.replace(3, 2, “x”)

將 s[3]開始長度 2 的字串改為 “x” “012345”→”012x5”

substr

回傳一段子字串 (public member function)

s.substr(3, 2)

從 s[3]開始取長度 2 的字串 s=”012345” , s.substr(3, 2)

=”34”

搜尋

find

回傳第一個找到該字串的位置 (public member function)

rfind

回傳最後一個找到該字串的位置 (public member function)

find_first_of

回傳第一個找到該字串其中一個字元的位置 (public member function)

find_first_not_of

回傳第一個找到不是該字串其中一個字元的位置 (public member function)

find_last_of

回傳最後一個找到該字串其中一個字元的位置 (public member function)

find_last_not_of

回傳最後一個找到不是該字串其中一個字元的位置 (public member function)

用法:s.find(“string”)

s=”012345” , s.find(“34”)

會回傳 3s.find(“string”, n)

s=”01230123”, s.find(“12” , 3)

會回傳 5 從 s[n] 開始 find, 預設為 0s.find_first_of(“chars”)

s=”012345” , s.find_first_of(“593”)

會回傳 3

常數

npos

[static]find 找不到時的回傳值 (public static member constant)

非成員函數

operator+

連結兩個字串 (function template)

operator == / != / < / > / <= / >=

依字典順序比較兩個字串 (function template)

std::swap (std::basic_string)

交換兩個字串 (function template)

用法: swap(s1, s2)

Input/output

operator>

輸出字串 (function template)

getline

取得一串字串 (function)

用法: getline(cin , s , ‘;’) 取得一串字串直到讀到 ‘;’ 為止

getline(cin, s) 第三個引數沒打則預設為換行 (取一行)