GESP C++三级官方考试大纲中,共有8条考点,之前已对前4个考点进行了总结梳理,6号考点是关于C++语言字符串应用的,属于基本语言语法和应用范围,网上的资料很多,本不想再赘述。但在孩子做题的过程中发现,对字符串应掌握哪些常用函数以及如何应用还不是很熟悉,因此将这部分内容单独整理出来,供孩子参考。
(6)掌握字符串及其函数的使用包括但不限于大小写转换、字符串搜索、分割、替换。
字符串常用函数详解
1. 基本操作函数
1.1 长度和容量
size()
/ length()
: 返回字符串长度empty()
: 判断字符串是否为空capacity()
: 返回当前分配的存储空间大小
1
2
3
4
| string str = "Hello";
cout << str.size() << endl; // 输出:5
cout << str.empty() << endl; // 输出:0(false)
cout << str.capacity() << endl; // 输出:15(可能因编译器不同而异)
|
1.2 访问和修改
at(pos)
: 访问指定位置字符(带边界检查)operator[]
: 访问指定位置字符(无边界检查)front()
: 访问第一个字符back()
: 访问最后一个字符
1
2
3
4
5
| string str = "Hello";
cout << str.at(0) << endl; // 输出:H
cout << str[1] << endl; // 输出:e
cout << str.front() << endl; // 输出:H
cout << str.back() << endl; // 输出:o
|
2. 字符串修改函数
2.1 添加和删除
append()
: 在末尾添加字符串push_back()
: 在末尾添加单个字符insert()
: 在指定位置插入erase()
: 删除指定位置的字符或子串clear()
: 清空字符串
1
2
3
4
5
| string str = "Hello";
str.append(" World"); // Hello World
str.push_back('!'); // Hello World!
str.insert(5, " C++"); // Hello C++ World!
str.erase(5, 4); // Hello World!
|
2.2 替换和子串
replace()
: 替换子串substr()
: 获取子串
1
2
3
| string str = "Hello World";
str.replace(6, 5, "C++"); // Hello C++
string sub = str.substr(6, 3); // 获取子串:"C++"
|
3. 字符串查找函数
3.1 查找函数
find()
: 从前向后查找子串,返回找到的第一个匹配子串的起始位置。返回值类型为 size_t
。若未找到则返回 string::npos
(通常是 -1)rfind()
: 从后向前查找子串,返回找到的最后一个匹配子串的起始位置。返回值类型为 size_t
。若未找到则返回 string::npos
find_first_of()
: 查找字符串中第一个与指定字符集合中任意字符匹配的位置。返回值类型为 size_t
。若未找到则返回 string::npos
find_last_of()
: 查找字符串中最后一个与指定字符集合中任意字符匹配的位置。返回值类型为 size_t
。若未找到则返回 string::npos
find_first_not_of()
: 查找字符串中第一个不在指定字符集合中的字符位置。返回值类型为 size_t
。若未找到则返回 string::npos
find_last_not_of()
: 查找字符串中最后一个不在指定字符集合中的字符位置。返回值类型为 size_t
。若未找到则返回 string::npos
1
2
3
4
| string str = "Hello World Hello";
cout << str.find("Hello") << endl; // 输出:0
cout << str.rfind("Hello") << endl; // 输出:12
cout << str.find_first_of("aeiou") << endl; // 输出:1(e的位置)
|
4. 字符串转换函数
4.1 大小写转换
toupper()
: 转换为大写tolower()
: 转换为小写
1
2
3
| string str = "Hello";
transform(str.begin(), str.end(), str.begin(), ::toupper); // HELLO
transform(str.begin(), str.end(), str.begin(), ::tolower); // hello
|
4.2 类型转换
to_string()
: 数值转字符串stoi()
, stol()
, stof()
, stod()
: 字符串转数值
1
2
3
| int num = 42;
string str = to_string(num); // 数字转字符串
int back = stoi("42"); // 字符串转数字
|
5. 字符串比较函数
compare()
: 比较字符串- 关系运算符:
==
, !=
, <
, >
, <=
, >=
1
2
3
4
| string str1 = "Hello";
string str2 = "World";
cout << str1.compare(str2) << endl; // 输出负数(str1 < str2)
cout << (str1 < str2) << endl; // 输出:1(true)
|
这些是 C++ 字符串操作中最常用的函数,掌握这些函数可以帮助更好地处理字符串相关的问题。
字符大小写判断和转换函数
1. 字符判断函数
C++ 提供了一系列用于判断字符类型的函数,这些函数定义在 <cctype>
头文件中:
isupper(c)
: 判断字符是否为大写字母islower(c)
: 判断字符是否为小写字母isalpha(c)
: 判断字符是否为字母isdigit(c)
: 判断字符是否为数字isalnum(c)
: 判断字符是否为字母或数字isspace(c)
: 判断字符是否为空白字符
1
2
3
4
5
6
7
| char c = 'A';
if (isupper(c)) {
cout << c << " 是大写字母" << endl;
}
if (isalpha(c)) {
cout << c << " 是字母" << endl;
}
|
2. 字符转换函数
2.1 单个字符转换
toupper(c)
: 将字符转换为大写tolower(c)
: 将字符转换为小写
1
2
3
| char c = 'a';
char upper_c = toupper(c); // 'A'
char lower_c = tolower('B'); // 'b'
|
所有代码已上传至Github:https://github.com/lihongzheshuai/yummy-code
GESP各级别考纲要点、知识拓展和练习题目清单详见C++学习项目主页
“luogu-”系列题目已加入洛谷Java、C++初学团队,作业清单,可在线评测,团队名额有限,欢迎加入。
“bcqm-”系列题目可在编程启蒙题库进行在线评测。