复试,string
std::string
是标准库提供的一个用于处理字符串的类,它位于 <string>
头文件中。相比于 C 风格的字符串(以 '\0'
结尾的字符数组),std::string
类提供了更方便、更安全且功能更强大的字符串操作方式。以下将从概念、内容、用法等方面详细介绍 std::string
。
概念
std::string
是一个模板类 std::basic_string<char>
的 typedef,它封装了字符序列,并提供了一系列成员函数来操作这些字符序列。使用 std::string
可以避免许多 C 风格字符串可能出现的问题,如缓冲区溢出、手动内存管理等。
内容
std::string
对象本质上是一个动态数组,它可以根据需要自动调整大小以容纳不同长度的字符串。它包含了一系列字符,并在内部管理这些字符的存储和生命周期。
用法
1. 包含头文件
要使用 std::string
,首先需要包含 <string>
头文件:
#include <string>
2. 声明和初始化
std::string
提供了多种初始化方式:
#include <iostream> #include <string> int main() { // 1. 默认初始化,创建一个空字符串 std::string s1; // 2. 使用字符串字面量初始化 std::string s2 = "Hello"; // 3. 使用另一个 std::string 对象初始化 std::string s3(s2); // 4. 指定重复字符初始化 std::string s4(5, 'A'); std::cout << "s1: " << s1 << std::endl; std::cout << "s2: " << s2 << std::endl; std::cout << "s3: " << s3 << std::endl; std::cout << "s4: " << s4 << std::endl; return 0; }
3. 字符串操作
连接字符串
可以使用 +
运算符或 +=
运算符来连接字符串:
#include <iostream> #include <string> int main() { std::string s1 = "Hello"; std::string s2 = " World"; std::string s3 = s1 + s2; // 使用 + 运算符连接 s1 += s2; // 使用 += 运算符连接 std::cout << "s3: " << s3 << std::endl; std::cout << "s1: " << s1 << std::endl; return 0; }
获取字符串长度
可以使用 size()
或 length()
成员函数来获取字符串的长度:
#include <iostream> #include <string> int main() { std::string s = "Hello"; std::cout << "字符串长度: " << s.size() << std::endl; std::cout << "字符串长度: " << s.length() << std::endl; return 0; }
访问字符串中的字符
可以使用下标运算符 []
或 at()
成员函数来访问字符串中的单个字符:
#include <iostream> #include <string> int main() { std::string s = "Hello"; std::cout << "第一个字符: " << s[0] << std::endl; std::cout << "第二个字符: " << s.at(1) << std::endl; return 0; }
[]
运算符不进行边界检查,而 at()
函数会进行边界检查,如果越界会抛出 std::out_of_range
异常。
查找子字符串
可以使用 find()
成员函数来查找子字符串在原字符串中的位置:
#include <iostream> #include <string> int main() { std::string s = "Hello World"; size_t pos = s.find("World"); if (pos != std::string::npos) { std::cout << "子字符串 'World' 的位置: " << pos << std::endl; } else { std::cout << "未找到子字符串" << std::endl; } return 0; }
std::string::npos
是一个静态常量,表示查找失败。
字符串截取
可以使用 substr()
成员函数来截取子字符串:
#include <iostream> #include <string> int main() { std::string s = "Hello World"; std::string sub = s.substr(6, 5); // 从位置 6 开始截取长度为 5 的子字符串 std::cout << "截取的子字符串: " << sub << std::endl; return 0; }
4. 字符串比较
可以使用 ==
、!=
、<
、>
等运算符来比较两个字符串的大小,也可以使用 compare()
成员函数进行比较:
#include <iostream> #include <string> int main() { std::string s1 = "apple"; std::string s2 = "banana"; if (s1 < s2) { std::cout << "s1 小于 s2" << std::endl; } int result = s1.compare(s2); if (result < 0) { std::cout << "s1 小于 s2" << std::endl; } return 0; }
5. 输入输出
可以使用 std::cin
和 std::cout
进行字符串的输入输出:
#include <iostream> #include <string> int main() { std::string s; std::cout << "请输入一个字符串: "; std::cin >> s; // 读取一个单词 std::cout << "你输入的字符串是: " << s << std::endl; std::string line; std::cout << "请输入一行字符串: "; std::getline(std::cin, line); // 读取一行字符串 std::cout << "你输入的一行字符串是: " << line << std::endl; return 0; }
std::cin >> s
会在遇到空格或换行符时停止读取,而 std::getline(std::cin, line)
会读取一整行字符串。
std::string
push_back
和 pop_back
操作
push_back
操作
功能
push_back
函数用于在 std::string
对象的末尾添加一个字符。这是一种动态增长字符串的方式,类似于在容器末尾添加元素。
用法
void push_back( char ch );
其中,ch
是要添加到字符串末尾的字符。
示例代码
#include <iostream> #include <string> int main() { std::string str = "Hello"; std::cout << "原始字符串: " << str << std::endl; // 使用 push_back 添加字符 str.push_back('!'); std::cout << "添加字符后的字符串: " << str << std::endl; return 0; }
代码解释
- 首先定义了一个
std::string
对象str
并初始化为"Hello"
。 - 然后调用
push_back
函数,将字符'!'
添加到str
的末尾。 - 最后输出添加字符后的字符串。
pop_back
操作
功能
pop_back
函数用于移除 std::string
对象末尾的字符。如果字符串为空,调用该函数会导致未定义行为,所以在调用前最好先检查字符串是否为空。
用法
void pop_back();
示例代码
#include <iostream> #include <string> int main() { std::string str = "Hello!"; std::cout << "原始字符串: " << str << std::endl; // 检查字符串是否为空 if (!str.empty()) { // 使用 pop_back 移除末尾字符 str.pop_back(); std::cout << "移除末尾字符后的字符串: " << str << std::endl; } return 0; }
代码解释
- 定义一个
std::string
对象str
并初始化为"Hello!"
。 - 使用
empty
函数检查字符串是否为空,避免对空字符串调用pop_back
导致未定义行为。 - 如果字符串不为空,调用
pop_back
函数移除末尾的字符'!'
。 - 输出移除末尾字符后的字符串。
insert
、erase
insert
函数
insert
函数用于在字符串的指定位置插入字符、字符串或字符序列。它有多种重载形式,以下是一些常见的用法示例:
#include <iostream> #include <string> int main() { std::string str = "Hello World"; // 在指定位置插入单个字符 str.insert(5, 1, ','); std::cout << "插入单个字符后: " << str << std::endl; // 在指定位置插入字符串 std::string insertStr = " beautiful"; str.insert(6, insertStr); std::cout << "插入字符串后: " << str << std::endl; // 在指定位置插入字符串的一部分 str.insert(17, insertStr, 1, 5); std::cout << "插入字符串的一部分后: " << str << std::endl; return 0; }
代码解释
str.insert(5, 1, ',')
:在位置 5 处插入 1 个字符','
。str.insert(6, insertStr)
:在位置 6 处插入整个字符串insertStr
。str.insert(17, insertStr, 1, 5)
:在位置 17 处插入insertStr
从索引 1 开始长度为 5 的子字符串。
erase
函数
erase
函数用于删除字符串中的部分字符。同样有多种重载形式,下面是示例代码:
#include <iostream> #include <string> int main() { std::string str = "Hello beautiful World"; // 删除指定位置开始的指定长度的字符 str.erase(6, 10); std::cout << "删除指定长度字符后: " << str << std::endl; // 删除指定位置的单个字符 str.erase(5); std::cout << "删除单个字符后: " << str << std::endl; // 删除从指定位置到字符串末尾的所有字符 str.erase(2); std::cout << "删除到末尾的字符后: " << str << std::endl; return 0; }
代码解释
str.erase(6, 10)
:从位置 6 开始删除长度为 10 的字符。str.erase(5)
:删除位置 5 处的单个字符。str.erase(2)
:删除从位置 2 到字符串末尾的所有字符。
字符串匹配
在 std::string
中,常见的字符串匹配方式有使用 find
、rfind
、find_first_of
、find_last_of
等函数,以下是详细介绍:
find
函数
用于查找子字符串或字符第一次出现的位置,如果找到则返回其位置,未找到则返回 std::string::npos
。
#include <iostream> #include <string> int main() { std::string str = "Hello World"; size_t pos = str.find("World"); if (pos != std::string::npos) { std::cout << "子字符串 'World' 第一次出现的位置: " << pos << std::endl; } else { std::cout << "未找到子字符串" << std::endl; } return 0; }
rfind
函数
用于查找子字符串或字符最后一次出现的位置,同样未找到返回 std::string::npos
。
#include <iostream> #include <string> int main() { std::string str = "Hello World, World"; size_t pos = str.rfind("World"); if (pos != std::string::npos) { std::cout << "子字符串 'World' 最后一次出现的位置: " << pos << std::endl; } else { std::cout << "未找到子字符串" << std::endl; } return 0; }
find_first_of
函数
查找字符串中第一个与指定字符集中任意字符匹配的位置。
#include <iostream> #include <string> int main() { std::string str = "Hello World"; std::string chars = "aeiou"; size_t pos = str.find_first_of(chars); if (pos != std::string::npos) { std::cout << "第一个元音字母出现的位置: " << pos << std::endl; } else { std::cout << "未找到元音字母" << std::endl; } return 0; }
find_last_of
函数
查找字符串中最后一个与指定字符集中任意字符匹配的位置。
#include <iostream> #include <string> int main() { std::string str = "Hello World"; std::string chars = "aeiou"; size_t pos = str.find_last_of(chars); if (pos != std::string::npos) { std::cout << "最后一个元音字母出现的位置: " << pos << std::endl; } else { std::cout << "未找到元音字母" << std::endl; } return 0; }
通过上述 insert
、erase
函数以及各种字符串匹配函数,你可以方便地对 std::string
对象进行修改和查找操作。
考研机试常用的数据结构