本课时有配套视频讲解,购买后即可观看(永久有效)
string类
一、课上练习
编程练习
二、知识总结
✨ string类的核心思想
string类是C++中一种带有操作的数据类型,可以用于表示和操作字符串。定义包含在头文件中,需要使用std命名空间来使用string类。相比字符数组,string类使用更加方便,不需要手动管理内存大小,支持丰富的内置操作。
✨ string类的定义、初始化及访问
string类有多种初始化方式,可以通过下标访问单个字符:
string定义与初始化代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str1; // 初始化为空
7 string str2 = ""; // 初始化为空
8 string str3(); // 初始化为空
9 string str4 = "hello"; // 初始化为:hello
10 string str5("world"); // 初始化为:world
11 string str6(5, 'a'); // 初始化为:aaaaa
12
13
14 cout << str4 << endl;
15 cout << str5[2] << endl;
16
17
18 return 0;
19}✨ string类常见操作
1. 获取字符串长度
获取字符串存储字符数目的方法:
- length函数
- size函数
获取字符串长度代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 // 字符串长度的获取
7 string str = "hello";
8 int length = str.length(); // 获取str的长度
9 cout << length << endl; // 输出结果为 5
10
11 int size = str.size(); // 获取str的长度
12 cout << size << endl; // 输出结果为 5
13
14 return 0;
15}2. 连接字符串
连接字符串的方法:使用+符号将多个字符串拼接在一起。
连接字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str1 = "hello";
7 string str2 = "world";
8 string str3 = str1 + " " + str2; // 连接三个字符串
9 cout << str3 << endl; // 输出结果为:"hello world"
10
11 return 0;
12}3. 追加字符串
在一个字符串后追加另外一个字符串的方法:
- +=符号
- append函数
追加字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str;
7
8 str = "hello"; // 修改str为字符串:"hello"
9 str += " world"; // 在str后加上字符串:" world"
10 cout << str << endl; // 输出结果为:"hello world"
11
12 str = "hello"; // 修改str为字符串:"hello"
13 char chs[10] = " world"; // 创建字符数组chs,并初始化为:" world"
14 str += chs; // 在str后加上字符串:" world"
15 cout << str << endl; // 输出结果为:"hello world"
16
17 return 0;
18}append函数追加字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str;
7
8 str = "hello"; // 修改str为字符串:"hello"
9 str.append(" world"); // 在str后加上字符串:" world"
10 cout << str << endl; // 输出结果为:"hello world"
11
12 str = "hello"; // 修改str为字符串:"hello"
13 char chs[10] = " world"; // 创建字符数组chs,并初始化为:" world"
14 str.append(chs); // 在str后加上字符串:" world"
15 cout << str << endl; // 输出结果为:"hello world"
16
17 str = "hello"; // 修改str为字符串:"hello"
18 str.append(3, '!'); // 在str后加上 3 个 '!' 字符
19 cout << str << endl; // 输出结果为:"hello!!!"
20
21 return 0;
22}4. 插入一段字符串
在一个字符串中间位置插入一段字符的方法:使用insert函数。
insert插入字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str;
7
8 str = "helrld"; // 修改str为字符串:"helrld"
9 str.insert(3, "lo wo"); // 在str下标为 3 的位置上,插入字符串:"lo wo"
10 cout << str << endl; // 输出结果为:"hello world"
11
12 str = "helrld"; // 修改str为字符串:"helrld"
13 char chs[10] = "lo wo"; // 创建字符数组chs,并初始化为:"lo wo"
14 str.insert(3, chs); // 在str下标为 3 的位置上,插入字符串:"lo wo"
15 cout << str << endl; // 输出结果为:"hello world"
16
17 str = "helloworld"; // 修改str为字符串:"helloworld"
18 str.insert(5, 3, '-'); // 在str下标为 5 的位置上,插入 3 个 '-' 字符
19 cout << str << endl; // 输出结果为:"hello---world"
20
21 return 0;
22}5. 删除一段字符串
删除一个字符串中某一段字符的方法:使用erase函数。
erase删除字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str;
7
8 str = "he123llo"; // 修改str为字符串:"he123llo"
9 str.erase(2, 3); // 删除str从下标为 2 开始的 3 个字符,即删除字符串"123"
10 cout << str << endl; // 输出结果为:"hello"
11
12 str = "hello123"; // 修改str为字符串:"hello123"
13 str.erase(5); // 删除str从下标为 5 开始的的所有字符,即删除字符串"123"
14 cout << str << endl; // 输出结果为:"hello"
15
16 return 0;
17}6. 获取子字符串
获取一个字符串子字符串的方法:使用substr函数。
substr获取子字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str;
7
8 str = "he123llo"; // 修改str为字符串:"he123llo"
9 string sub_str1 = str.substr(2, 3); // 获取从str下标为 2 开始的 3 个字符,即字符串"123"
10 cout << sub_str1 << endl; // 输出结果为:"123"
11
12 str = "hello123"; // 修改str为字符串:"hello123"
13 string sub_str2 = str.substr(5); // 获取从str下标为 5 开始的所有字符,即字符串"123"
14 cout << sub_str2 << endl; // 输出结果为:"123"
15
16 return 0;
17}7. 字符及字符串查找
查找字符或子字符串的方法:使用find和rfind函数。find从左向右查找第一个匹配项,rfind从右向左查找最后一个匹配项。如果找不到,返回string::npos。
find查找字符串代码示例
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6 string str = "hello"; // 初始化str为字符串:"hello"
7 size_t pos; // 在pos中存储查找到的内容
8
9 pos = str.find('l'); // 查找str字符串中的第一个字符'l'
10 if (pos == string::npos) {
11 cout << "not find" << endl;
12 } else {
13 cout << pos << endl; // 输出结果为 2
14 }
15
16 pos = str.find("l"); // 查找str字符串中的第一个字符串"l"
17 if (pos == string::npos) {
18 cout << "not find" << endl;
19 } else {
20 cout << pos << endl; // 输出结果为 2
21 }
22
23 pos = str.find("l", 3); // 从str下标为3的字符开始,查找后续字符串中的第一个字符串"l"
24 if (pos == string::npos) {
25 cout << "not find" << endl;
26 } else {
27 cout << pos << endl; // 输出结果为 3
28 }
29
30 pos = str.find("123", 3); // 从str下标为3的字符开始, 查找后续字符串中的第一个字符串"123"
31 if (pos == string::npos) {
32 cout << "not find" << endl; // 输出结果为 not find
33 } else {
34 cout << pos << endl;
35 }
36
37 pos = str.rfind("l"); // 查找str字符串中的最后一个字符串"l"
38 if (pos == string::npos) {
39 cout << "not find" << endl;
40 } else {
41 cout << pos << endl; // 输出结果为 3
42 }
43
44 return 0;
45}✨ string类的执行示例
问题:将字符串中每个字母向后移动k位(超过'z'则循环回'a')。例如 k=3 时,'a'→'d','x'→'a'。
凯撒加密核心逻辑
string str = "xyz";
int k = 3;
for (int i = 0; i < str.length(); i++) {
str[i] = (str[i] - 'a' + k) % 26 + 'a';
}逐步执行:
| 步骤 | i | str[i] | str[i]-'a' | +k | %26 | +'a' | 结果 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 'x' | 23 | 26 | 0 | 'a' | 'a' |
| 2 | 1 | 'y' | 24 | 27 | 1 | 'b' | 'b' |
| 3 | 2 | 'z' | 25 | 28 | 2 | 'c' | 'c' |
最终结果:"abc"
关键技巧: 先减去 'a' 将字母映射到 0-25 的范围,加上偏移后取模 26 实现循环,最后加回 'a'。
✨ string类的常见错误
- 头文件写错:string 类的头文件是
,而不是。是 C 风格字符串函数(strlen、strcpy等)的头文件。不过如果使用则两者都包含。 - string 和字符数组混用时的陷阱:string 可以用
+连接,但字符数组不行。"hello" + "world"不会拼接两个字符串字面量,因为它们是 char* 类型。需要至少有一个是 string 类型:string("hello") + "world"。 - find 返回值判断错误:find 找不到时返回
string::npos,它是一个很大的无符号整数。不要用if (str.find("abc") >= 0)来判断是否找到,因为 npos 也大于 0。正确写法是if (str.find("abc") != string::npos)。 - erase 和 substr 的参数含义不同于预期:第二个参数是长度而不是结束位置。
str.erase(2, 3)是从下标2开始删除3个字符,不是删除下标2到3的字符。 - 修改字符串时越界:通过下标
str[i]访问时,i 不能超过str.length()-1。虽然 string 比字符数组安全,但下标越界依然是未定义行为。
三、课后练习
基础知识练习
- string类 - 选择题## 编程练习