string类-知识总结
string类的概念
string类是一种带有操作的数据类型,可以用于表示和操作字符串。定义包含在头文件
string类的定义、初始化及访问 代码示例
1#include <iostream>
2#include <cstrting>
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类常见操作 获取字符串长度 获取字符串存储字符数目的方法: 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}连接字符串 连接字符串的方法:+符号
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}追加字符串 在一个字符串后追加另外一个字符串的方法: +=符号 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}
19#include <iostream>
20#include <cstring>
21using namespace std;
22
23int main() {
24 string str;
25
26 str = "hello"; // 修改str为字符串:"hello"
27 str.append(" world"); // 在str后加上字符串:" world"
28 cout << str << endl; // 输出结果为:"hello world"
29
30 str = "hello"; // 修改str为字符串:"hello"
31 char chs[10] = " world"; // 创建字符数组chs,并初始化为:" world"
32 str.append(chs); // 在str后加上字符串:" world"
33 cout << str << endl; // 输出结果为:"hello world"
34
35 str = "hello"; // 修改str为字符串:"hello"
36 str.append(3, '!'); // 在str后加上 3 个 '1' 字符
37 cout << str << endl; // 输出结果为:"hello!!!"
38
39 return 0;
40}插入一段字符串 在一个字符串中间位置插入一段字符的方法: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为字符串:"heo"
18 str.insert(5, 3, '-'); // 在str下标为 5 的位置上,插入 3 个 '-' 字符
19 cout << str << endl; // 输出结果为:"hello---world"
20
21 return 0;
22}删除一段字符串 删除一个字符串中某一段字符的方法: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}获取子字符串 获取一个字符串子字符串的方法: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; // 输出结果为:"hello"
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}字符及字符串查找
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; // 输出结果为 3
35 }
36
37 pos = str.rfind("l") // 查找str字符串中的最后一个字符串"l"
38 if (pos == string::npos) {
39 cout << "not find" << endl; // 输出结果为 not find
40 } else {
41 cout << pos << endl; // 输出结果为 3
42 }
43
44 return 0;
45}错误的题目要弄懂,不会的千万要来问老师⚠️
总结作业非常重要,请务必认真完成✔️
考试只有一次提交机会,请务必本地检查正确后再提交❇️
请在AC后或者思考15分钟没有进展后查看,不可抄代码❇️
string类-选择题 在C++中,如何初始化一个空的string对象?
A. string str = "";
B. string str();C. string str; D. A和C都是
下面C++代码执行后不能输出“GESP”的是?
A. string str("GESP"); cout << str << endl;
B. string str"GESP"; cout << str << endl;
C. string str("GESP"); cout << str[1] << str[2] << str[3] << str[4] << endl;
D. string str{"GESP"}; cout << str << endl;现在有两个字符串str1 = "hello"; str2 = "world",如何将字str2连接在str1之后? A. str1 + str2;
B. str1 += str2;
C. str1.append(str2);D. A和B都是
在C++中,对于string str = 'hello',下列哪个函数可以返回'o'?
A. str.substr(4);
B. str.substr(5,1);
C. str.substr(4,1);D. A和C都是
在string 中,如何查找子字符串的位置?
A. str.find(sub_str);
B. str.indexOf(sub_str);
C. str.search(sub_str);
D. str.position(sub_str);如何将字符插入到string的特定位置?
A. str.insert(position, character);
B. str.add(position, character);
C. str.push_back(character);
D. str += character;在C++中,对于string str='hello', str.find('l',3)将返回什么? A. 2; B. 3; C. 4; D. string::npos;
以下哪个选项不是比较两个string类变量的字典顺序?
A. str1 < str2;
B. str1 > str2;
C. str1 == str2;
D. strcmp(str1, str2);在下列代码的横线处填写( ),可以使得输出是:GESP IS INTERESTING? image.png
A. str[i] += 'a' - 'A'
B. str[i] += 20;
C. str[i] += 'A' - 'a';D. 无法实现
执行下面的C++代码后输出的值为多少( )? image.png
A. 1 B. 2 C. 6 D. 14
string类-选择题答案 D 选项A中string str = "";这种方法使用了一个空字符串字面值来初始化string 对象 str。这是一种非常直接的方法,通过赋值操作创建了一个空的字符串对象。 选项B中string str();看起来像是在试图默认构造一个string 对象,但它实际上是一个函数声明,而不是对象的定义。这里声明了一个名为 str、返回string 并且不接受任何参数的函数。这不是对象的初始化,而是一个易于混淆的语法错误。 选项C中 string str; 这种方式使用string 的默认构造函数来创建一个空的字符串对象。std::string 的默认构造函数确保创建的字符串是空的,这是初始化空string 对象的推荐方式。选项 A 也会创建一个空的字符串,但实际上是通过复制一个空的字面值字符串来初始化string 对象,这在功能上是相同的,但从技术上讲,它涉及复制操作。因此,最简洁和直接的方法是使用选项 C C 选项A、选项B、选项D都是对string变量的初始化方式,需要注意的是,对于选项A,只有括号内有字符串的时候才能创建变量,如果是string str();的书写形式,则无法创建变量。 D append函数也可以将一个字符串链接在另外一个字符串之后,具体使用方法请参照知识总结介绍。 D substr(4,1)第一个参数表示子字符串在原字符串中的起始位置,第二个参数表示子字符串长度,如果不传第二个参数,则一直截取到原字符串结束的位置。因为字符长度是5,位置从0开始,最后一个位置为4,所以A、C都返回最后一个字符o A find 方法用于查找子字符串的位置。 A insert 方法用于在指定位置插入字符或子字符串。 B find('l',3)指从3的位置开始找字符'l',hello位置从0开始是h D 选项D是用来比较字符数组的大小的。 C 如果字符是小写,则减去小写a的ASCII码再加上大写A的ASCII码即可完成字符从小写到大写的转换。 B find函数会输出查找到的第一个字符串的位置。
L2075题解+参考代码 题目链接 整理药名:L2075
题解 题目要求将医生书写混乱的药品名整理成统一规范的格式,即药品名的第一个字符如果是字母要大写,其他字母小写。可以使用字符串操作来完成这个任务。
算法步骤 读取输入:读取一个整数 n,表示有 n 个药品名。接着读取 n 个药品名。 处理每个药品名: 遍历每个药品名,检查第一个字符是否为字母,如果是则转换为大写。 将剩余的字符转换为小写。 输出结果:输出处理后的药品名。 示例代码 这里是使用 C++ 实现的示例代码:
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 // 读取输入
7 int n;
8 cin >> n;
9 string drugs[100]; // 假设最多有 100 个药品名
10
11 for (int i = 0; i < n; ++i) {
12 cin >> drugs[i];
13 }
14
15 // 处理每个药品名
16 for (int i = 0; i < n; ++i) {
17 // 将第一个字符转换为大写
18 if (drugs[i][0] >= 'a' && drugs[i][0] <= 'z') {
19 drugs[i][0] = drugs[i][0] - ('a' - 'A');
20 }
21
22 // 将其余字符转换为小写
23 for (int j = 1; j < drugs[i].length(); ++j) {
24 if (drugs[i][j] >= 'A' && drugs[i][j] <= 'Z') {
25 drugs[i][j] = drugs[i][j] + ('a' - 'A');
26 }
27 }
28 }
29
30 // 输出结果
31 for (int i = 0; i < n; ++i) {
32 cout << drugs[i] << endl;
33 }
34
35 return 0;
36}L2076题解+参考代码 题目链接 字符串的插入:L2076
题解 题目要求将一个字符串 s1 插入到另一个字符串 s2 的第 p 个字符之后。我们可以使用字符串操作来完成这个任务。
算法步骤 读取输入:读取字符串 s1 和 s2,以及位置 p。 插入字符串:使用 insert 方法将 s2 插入到 s1 的指定位置。 输出结果:输出新字符串。 示例代码 这里是使用 C++ 实现的示例代码:
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 // 读取输入
7 string s1, s2;
8 int p;
9 cin >> s1 >> s2 >> p;
10
11 // 插入字符串
12 s1.insert(p, s2);
13
14 // 输出结果
15 cout << s1 << endl;
16
17 return 0;
18}