首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C/C++ 字符串操作大全:C 風格函數、std::string 完整 API 與緩沖區管理

     編輯:關於C++

字符串處理是 C/C++ 開發的高頻需求。本文匯總 C 標準庫 <cstring> 常用函數、C++ std::basic_string 完整成員函數、字符特征類 char_traits、緩沖區管理及輸入迭代器尾端定義,附帶函數原型、參數說明與示例代碼。

一、C 風格字符串函數速查表

函數功能備註
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)轉 doubleendptr 指向未解析部分
strtol(s, &endptr, base)轉 longbase=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)。

二、std::string 完整 API (C++11/14/17/20)

1. 字符特征類 char_traits

封裝字符元素通用行為,供 basic_string 使用:

  • 類型定義:char_type, int_type, pos_type, off_type, state_type
  • 核心操作:assign/eq/lt/compare/length/copy/move/assign_fill/to_int_type/to_char_type/eq_int_type/eof/not_eof

2. 初始化方式 (13 種)

  1. 默認構造
  2. 分配器構造
  3. 拷貝構造
  4. 區間拷貝 [_Roff, _Roff + _Count)
  5. 區間拷貝 + 分配器
  6. C 字符串 [_Ptr, null)
  7. C 字符串 + 長度 [_Ptr, _Ptr + _Count)
  8. C 字符串 + 分配器
  9. C 字符串 + 長度 + 分配器
  10. _Count * _Ch 填充
  11. 填充 + 分配器
  12. 迭代器區間 [_ItF, _ItL)
  13. 迭代器區間 + 分配器

單字符不可直接構造,但支持 operator=、operator+= 字符賦值/追加。

3. 區間有效性

  • 下標越界:無異常,行為未定義(底層緩沖區直接訪問)
  • 他串作為右值:計數超過串長按串長處理
  • size_type 實為 size_t,npos = static_cast<size_type>(-1) 表示無效索引

4. 比較函數 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);

5. 附加數據

  • operator+= / push_back(c) / append() (6 種重載)
  • insert() (9 種重載):位置+字符串/C串/填充/迭代器區間
  • operator+ 非成員重載:s+s, s+cstr, s+c, cstr+s, c+s

6. 查找/替換/清除

類別函數變體
查找findrfind/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)

7. 取出字符串

  • c_str():返回 const char* (以 \0 結尾)
  • data():C++11 起同 c_str(),C++17 保證連續、以 \0 結尾
  • copy(ptr, count, pos=0):復制到用戶緩沖區(不加 \0)
  • substr(pos, count):返回子串副本

8. 緩沖區管理 (類 vector)

  • 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

四、示例代碼片段

strcpy/strncpy

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

strcat/strncat

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

strcmp/strlen

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

memset/memcpy/memmove

char buf[100];
memset(buf, 'A', 10); // 前 10 字節設為 'A'
memcpy(buf+10, "Hello", 6); // 復制
memmove(buf+5, buf, 15); // 重疊安全移動
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved