5.1 C++ string vs C 字符串
C++ 引入了 std::string 类,大大简化了字符串处理。
| 特性 | C 语言 | C++ |
|---|---|---|
| 类型 | char[] 或 char* | std::string |
| 结束标志 | '\0' | 不需要(自动管理) |
| 内存管理 | 手动 malloc/free | 自动管理 |
| 连接操作 | strcat(危险) | + 运算符(安全) |
| 大小计算 | strlen(每次遍历) | length()(缓存值) |
| 安全性 | 容易缓冲区溢出 | 自动边界检查 |
5.2 引入 string 头文件
xxxxxxxxxx// 注意:不是 string.h注意:
string.h是 C 语言的头文件,string是 C++ 的头文件。
5.3 创建 string 对象
xxxxxxxxxx/* string-create-objects.cpp */using namespace std;
int main() { string s1; // 空字符串 string s2 = "c++"; // 初始化为 "c++" string s3 = s2; // 复制构造 string s4("hello"); // 构造函数方式 string s5(5, 's'); // 5 个 's':sssss cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; cout << "s4: " << s4 << endl; cout << "s5: " << s5 << endl; return 0;}输出:
xxxxxxxxxxs1:s2: c++s3: c++s4: hellos5: sssss
5.4 string 运算符
| 运算符 | 说明 | 示例 |
|---|---|---|
+ | 字符串连接 | "hello" + "world" → "helloworld" |
+= | 追加字符串 | s += "!" |
== | 比较相等 | s1 == s2 |
!= | 比较不相等 | s1 != s2 |
<, <=, >, >= | 字典序比较 | s1 < s2 |
[] | 访问字符(下标) | s[0] 获取第一个字符 |
= | 赋值 | s1 = s2 |
xxxxxxxxxx/* string-ops.cpp */using namespace std;
int main() { string s1 = "hello ", s2, s3; cin >> s2; // 输入字符串(遇到空格停止) s3 = s1 + s2; // 连接字符串 cout << s3 << endl; s1 += {'s', 'y', 's', 'u'}; // 追加字符 cout << s1 << endl; // 输出:hello sysu // 比较 string a = "apple", b = "banana"; cout << (a < b) << endl; // 1 (true),因为 'a' < 'b' // 访问字符 cout << s1[0] << endl; // h return 0;}5.5 string 常用成员函数
5.5.1 基本属性
| 函数 | 返回值 | 说明 |
|---|---|---|
length() | size_t | 返回字符串长度 |
size() | size_t | 返回字符串长度(同 length) |
c_str() | const char* | 转换为 C 风格字符串(以 \0 结尾) |
empty() | bool | 判断是否为空 |
xxxxxxxxxx/* str-method-basic.cpp */using namespace std;
int main() { string s = "sysu-computer"; int len = s.length(); const char* cs = s.c_str(); cout << cs << " len is " << len << endl; // 输出:sysu-computer len is 13 cout << "empty: " << s.empty() << endl; // 0 (false) return 0;}5.5.2 查找函数
| 函数 | 说明 |
|---|---|
find(str) | 查找 str 首次出现的位置(返回 size_t) |
rfind(str) | 查找 str 最后一次出现的位置 |
find_first_of(chars) | 查找第一个出现的指定字符 |
find_last_of(chars) | 查找最后一个出现的指定字符 |
注意:如果未找到,find 返回 string::npos(-1)
xxxxxxxxxxusing namespace std;
int main() { string s = "sysu-computer"; size_t pos = s.find("computer"); cout << "Found at position: " << pos << endl; // 5 size_t notFound = s.find("python"); if (notFound == string::npos) { cout << "Not found!" << endl; } return 0;}5.5.3 替换函数
xxxxxxxxxx/* str-method-find.cpp */using namespace std;
int main() { string s = "sysu-computer"; // 替换 "computer" 为 "software" int pos = s.find("computer"); s.replace(pos, 8, "software"); cout << s << endl; // 输出:sysu-software return 0;}5.5.4 追加、插入、删除
| 函数 | 说明 |
|---|---|
append(str) | 追加字符串 |
append(count, ch) | 追加 count 个字符 ch |
insert(index, str) | 在 index 位置插入字符串 |
insert(index, count, ch) | 在 index 位置插入 count 个字符 |
erase(index, count) | 从 index 开始删除 count 个字符 |
clear() | 清空字符串 |
xxxxxxxxxx/* str-method-insert.cpp */using namespace std;
int main() { string s = "ysu"; // 链式调用 s.insert(0, 1, 'S') // 在位置 0 插入 'S' → "Sysu" .append(1, '-') // 追加 '-' → "Sysu-" .append("Xcomputer"); // 追加 "Xcomputer" → "Sysu-Xcomputer" s.erase(5, 1); // 删除位置 5 开始的 1 个字符 → "Sysu-Computer" cout << s << endl; // 输出:Sysu-Computer return 0;}5.5.5 子串、比较、复制
| 函数 | 说明 |
|---|---|
substr(pos, count) | 返回从 pos 开始、长度为 count 的子串 |
compare(str) | 比较两个字符串,返回 0(相等)、正值或负值 |
compare(pos, count, str) | 比较从 pos 开始的子串与 str |
copy(dest, pos, count) | 复制到字符数组 dest |
swap(other) | 与 other 交换内容 |
xxxxxxxxxx/* str-method-substr.cpp */using namespace std;
int main() { string s = "Sysu-Computer"; // 获取子串 cout << s.substr(0, 4) << " "; // 输出:Sysu // 比较 if (s.compare(5, 8, "Computer") == 0) { cout << "OK!" << endl; } // 复制到 C 字符串 char buf[20]; s.copy(buf, 4, 0); buf[4] = '\0'; // 需要手动添加结束符 cout << buf << endl; // Sysu // 交换 string a = "hello", b = "world"; a.swap(b); cout << a << " " << b << endl; // world hello return 0;}5.6 C++11 新增函数
| 函数 | 说明 |
|---|---|
stoi(str) | 字符串转整数 |
stol(str) | 字符串转长整数 |
stod(str) | 字符串转双精度浮点数 |
to_string(num) | 数字转字符串 |
xxxxxxxxxxusing namespace std;
int main() { string s = "12345"; int num = stoi(s); // 12345 double d = stod("3.14"); // 3.14 string s2 = to_string(42); // "42" cout << num << " " << d << " " << s2 << endl; return 0;}5.7 字符串遍历
xxxxxxxxxxusing namespace std;
int main() { string s = "hello"; // 方法 1:下标访问 for (size_t i = 0; i < s.length(); i++) { cout << s[i] << " "; } cout << endl; // 方法 2:范围 for 循环(C++11) for (char c : s) { cout << c << " "; } cout << endl; // 方法 3:迭代器 for (auto it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; return 0;}26,869字