字符串處理是 C/C++ 開發的高頻需求。本文匯總 C 標準庫 <cstring> 常用函數、C++ std::basic_string 完整成員函數、字符特征類 char_traits、緩沖區管理及輸入迭代器尾端定義,附帶函數原型、參數說明與示例代碼。
| 函數 | 功能 | 備註 |
|---|---|---|
strcpy(dst, src) | 復制字符串 | dst 必須足夠大 |
strncpy(dst, src, n) | 復制前 n 字符 | 不足 n 補 \0,超過不加 \0 |
strcat(dst, src) | 追加字符串 | dst 需預留空間 |
strncat(dst, src, n) | 追加前 n 字符 | 自動添加 \0 |
strlen(s) | 字符串長度 | 不含 \0 |
strcmp(s1, s2) | 比較字符串 | 返回 <0/=0/>0 |
strcasecmp | 忽略大小寫比較 | POSIX 擴展 |
strncmp(s1, s2, n) | 比較前 n 字符 | |
strchr(s, c) | 正向查找字符 | 返回指針/NULL |
strrchr(s, c) | 反向查找字符 | |
strstr(s1, s2) | 查找子串 | |
strpbrk(s, set) | 查找 set 中任一字符 | |
strspn(s, set) | 首個不在 set 的偏移 | |
strcspn(s, set) | 首個在 set 的偏移 |
strtod(s, &endptr) | 轉 double | endptr 指向未解析部分 |
strtol(s, &endptr, base) | 轉 long | base=0 自動識別 0x/0 前綴 |
atoi/atol/atof | 簡易轉 int/long/double | 無錯誤檢測,不推薦 |
isalpha/isdigit/isalnum/isspace/isupper/islower/isxdigit/iscntrl/ispunct/isprint/isgraph — 均接受 int (轉 unsigned char 或 EOF),返回非零/零。
memset(dst, c, n) | 設置前 n 字節為 c | |
memcpy(dst, src, n) | 復制 n 字節 | 不處理重疊 |
memmove(dst, src, n) | 復制 n 字節 | 正確處理重疊 |
memchr(s, c, n) | 查找字符 | |
memcmp(s1, s2, n) | 比較 n 字節 | |
memicmp | 忽略大小寫比較 | 非標準 |
strrev(反轉)、strupr/strlwr(大小寫轉換)、strset/strnset(填充)、strtok(分割)、strdup(複製並 malloc)。
char_traits封裝字符元素通用行為,供 basic_string 使用:
char_type, int_type, pos_type, off_type, state_typeassign/eq/lt/compare/length/copy/move/assign_fill/to_int_type/to_char_type/eq_int_type/eof/not_eof[_Roff, _Roff + _Count)[_Ptr, null)[_Ptr, _Ptr + _Count)_Count * _Ch 填充[_ItF, _ItL)單字符不可直接構造,但支持 operator=、operator+= 字符賦值/追加。
size_type 實為 size_t,npos = static_cast<size_type>(-1) 表示無效索引compare (6 種重載)// 返回 -1/0/1 s.compare(s2); s.compare(pos, count, s2); s.compare(pos, count, s2, pos2, count2); s.compare(pos, count, cstr); s.compare(pos, count, cstr, count2);
operator+= / push_back(c) / append() (6 種重載)insert() (9 種重載):位置+字符串/C串/填充/迭代器區間operator+ 非成員重載:s+s, s+cstr, s+c, cstr+s, c+s| 類別 | 函數 | 變體 |
|---|---|---|
| 查找 | find | rfind/find_first_of/find_last_of/find_first_not_of/find_last_not_of |
| 替換 | replace (10 種) | 位置/迭代器 + 字符串/C串/填充/區間 |
| 刪除 | erase (3 種) | erase(pos, count), erase(iter), erase(first, last) |
c_str():返回 const char* (以 \0 結尾)data():C++11 起同 c_str(),C++17 保證連續、以 \0 結尾copy(ptr, count, pos=0):復制到用戶緩沖區(不加 \0)substr(pos, count):返回子串副本size()/length():有效長度capacity():分配容量reserve(n):預留容量resize(n, c='\0'):調整長度max_size():理論最大長度shrink_to_fit() (C++11):釋放多餘容量istream_iterator 默認構造函數持有空流指針,operator== 比較流指針地址,使默認構造的迭代器成為「尾端哨兵」。流讀取失敗時指針置空,自動匹配尾端。
std::istream_iterator<int> begin(std::cin), end; // end 為默認構造 std::vector<int> v(begin, end); // 讀取至 EOF
char str1[10] = "TsinghuaOK"; char str2[10] = "Computer"; std::cout << strcpy(str1, str2) << std::endl; // Computer char str3[10] = "Tsinghua "; char str4[10] = "Computer"; std::cout << strncpy(str3, str4, 3) << std::endl; // Comnghua
char str1[] = "Tsinghua "; char str2[] = "Computer"; std::cout << strcat(str1, str2) << std::endl; // Tsinghua Computer char str3[] = "Tsinghua "; char str4[] = "Computer"; std::cout << strncat(str3, str4, 3) << std::endl; // Tsinghua Com
char buf1[] = "aaa", buf2[] = "bbb", buf3[] = "ccc";
std::cout << (strcmp(buf2, buf1) > 0 ? "greater" : "less") << std::endl; // greater
std::cout << strlen("Welcome") << std::endl; // 7
char buf[100]; memset(buf, 'A', 10); // 前 10 字節設為 'A' memcpy(buf+10, "Hello", 6); // 復制 memmove(buf+5, buf, 15); // 重疊安全移動