std::string 是 C++ 標準庫提供的強大字串類別,封裝了記憶體管理與豐富操作介面,可視為字元容器使用。本文整理所有成員函數供查閱。
string(); // 空字串 string(size_type n, char c); // n 個 c string(const char* s); // C 風格字串 string(const char* s, size_type n); // C 字串前 n 字元 string(const string& str, size_type pos, size_type n = npos); // 子串 templatestring(InputIt first, InputIt last); // 迭代器範圍
== != < > <= >= + += []
string& append(const string& str); string& append(const char* s); string& append(const string& str, size_type pos, size_type n); string& append(const char* s, size_type n); string& append(size_type n, char c); string& append(InputIt first, InputIt last); void push_back(char c);
string& assign(const string& str); string& assign(const char* s); string& assign(const char* s, size_type n); string& assign(const string& str, size_type pos, size_type n); string& assign(size_type n, char c); string& assign(InputIt first, InputIt last);
int compare(const string& str) const; int compare(size_type pos, size_type n, const string& str) const; int compare(size_type pos1, size_type n1, const string& str, size_type pos2, size_type n2) const; int compare(const char* s) const; int compare(size_type pos, size_type n, const char* s) const; int compare(size_type pos, size_type n, const char* s, size_type n2) const; // 回傳 <0, 0, >0 分別對應 <, ==, >
iterator erase(iterator first, iterator last); iterator erase(iterator pos); string& erase(size_type pos = 0, size_type n = npos);
iterator insert(iterator pos, char c); string& insert(size_type pos, const string& str); string& insert(size_type pos, const char* s); string& insert(size_type pos, const string& str, size_type pos2, size_type n); string& insert(size_type pos, const char* s, size_type n); string& insert(size_type pos, size_type n, char c); void insert(iterator pos, size_type n, char c); void insert(iterator pos, InputIt first, InputIt last);
string& replace(size_type pos, size_type n, const string& str); string& replace(size_type pos1, size_type n1, const string& str, size_type pos2, size_type n2); string& replace(size_type pos, size_type n, const char* s); string& replace(size_type pos, size_type n1, const char* s, size_type n2); string& replace(size_type pos, size_type n1, size_type n2, char c); string& replace(iterator first, iterator last, const string& str); string& replace(iterator first, iterator last, const char* s); string& replace(iterator first, iterator last, const char* s, size_type n); string& replace(iterator first, iterator last, size_type n, char c);
size_type find(const string& str, size_type pos = 0) const; size_type find(const char* s, size_type pos = 0) const; size_type find(const char* s, size_type pos, size_type n) const; size_type find(char c, size_type pos = 0) const; size_type rfind(const string& str, size_type pos = npos) const; size_type rfind(const char* s, size_type pos = npos) const; size_type rfind(const char* s, size_type pos, size_type n) const; size_type rfind(char c, size_type pos = npos) const; size_type find_first_of(const string& str, size_type pos = 0) const; size_type find_last_of(const string& str, size_type pos = npos) const; size_type find_first_not_of(const string& str, size_type pos = 0) const; size_type find_last_not_of(const string& str, size_type pos = npos) const; // 找不到回傳 string::npos (通常為 -1)
| 函數 | 說明 |
|---|---|
at(pos) | 帶邊界檢查的存取,越界拋出 out_of_range |
front()/back() | 首/尾字元參考 |
c_str()/data() | 取得 C 風格字串指標 |
size()/length() | 字串長度 |
capacity() | 目前配置容量 |
empty() | 是否為空 |
clear() | 清空內容 |
resize(n, c) | 調整大小,不足填 c |
reserve(n) | 預留容量 |
substr(pos, n) | 取得子串 |
swap(str) | 交換內容 |
begin()/end() | 迭代器 |
rbegin()/rend() | 反向迭代器 |
get_allocator() | 取得配置器 |
copy(buf, n, pos) | 複製到緩衝區 |
#include#include #include using namespace std; int main() { string s1; cin >> s1; // 遇空白停止 getline(cin, s1); // 整行讀取 string s2 = "hello"; string s3(5, 'x'); // "xxxxx" string s4(s2, 1, 3); // "ell" char c = s2[1]; // 'e' c = s2.at(2); // 'l' (含檢查) cout << "長度: " << s2.length() << endl; cout << "容量: " << s2.capacity() << endl; s2 += " world"; // 連接 s2.append("!"); if (s2 == "hello world!") // 比較 cout << "相等" << endl; string sub = s2.substr(6, 5); // "world" s2.replace(6, 5, "C++"); // "hello C++!" s2.insert(5, ","); // "hello, C++!" s2.erase(5, 1); // 刪除 ',' // 字串流分割 string line("hello this is a test"); istringstream iss(line); string word; while (iss >> word) cout << word << '|'; return 0; }