<string>
<string>
是 C++ 标准库中的一个头文件,提供了 std::string
类,用于处理字符串操作。std::string
封装了对动态字符数组的管理,提供了丰富的字符串处理功能,如拼接、查找、替换等。它是 C++ 中进行字符串处理的主要工具。
1. std::string
类
std::string
是一个类模板,用于处理和操作字符串。它的基本语法如下:
#include <string>
class string {
public:
// 构造函数
string(); // 默认构造函数
string(const string& other); // 拷贝构造函数
string(string&& other) noexcept; // 移动构造函数
string(const char* s); // 从 C 字符串构造
string(const std::string& s, size_t pos, size_t len = npos); // 子串构造
// 赋值操作
string& operator=(const string& other); // 赋值运算符
string& operator=(string&& other) noexcept; // 移动赋值运算符
string& operator=(const char* s); // 从 C 字符串赋值
// 基本操作
size_t size() const; // 返回字符串长度
size_t length() const; // 返回字符串长度
bool empty() const; // 判断字符串是否为空
void clear(); // 清空字符串
// 字符串访问
char& operator[](size_t index); // 访问字符
const char& operator[](size_t index) const; // 访问字符
char& at(size_t index); // 访问字符,带范围检查
const char& at(size_t index) const; // 访问字符,带范围检查
// 字符串操作
string& append(const string& str); // 拼接字符串
string& append(const char* s); // 拼接 C 字符串
string& append(const char* s, size_t n); // 拼接 C 字符串的一部分
string substr(size_t pos = 0, size_t len = npos) const; // 提取子串
size_t find(const string& str, size_t pos = 0) const; // 查找子串
size_t rfind(const string& str, size_t pos = npos) const; // 从后向前查找子串
// 比较操作
int compare(const string& str) const; // 比较字符串
bool operator==(const string& other) const; // 相等比较
bool operator!=(const string& other) const; // 不等比较
bool operator<(const string& other) const; // 小于比较
bool operator>(const string& other) const; // 大于比较
bool operator<=(const string& other) const; // 小于等于比较
bool operator>=(const string& other) const; // 大于等于比较
// 输入输出
friend std::ostream& operator<<(std::ostream& os, const string& str);
friend std::istream& operator>>(std::istream& is, string& str);
// 其他操作
// ...
};
2. 基本操作
创建和初始化
可以通过多种方式创建和初始化 std::string
对象。
#include <iostream>
#include <string>
int main() {
std::string s1; // 默认构造函数,空字符串
std::string s2("Hello, world!"); // 从 C 字符串构造
std::string s3(s2); // 拷贝构造函数
std::string s4(s2, 7, 5); // 从子串构造
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;
}
字符串操作
std::string
提供了多种字符串操作函数,如拼接、查找、替换等。
#include <iostream>
#include <string>
int main() {
std::string s1 = "Hello";
std::string s2 = "World";
// 拼接字符串
s1.append(", ");
s1.append(s2);
std::cout << "s1: " << s1 << std::endl;
// 查找子串
size_t pos = s1.find("World");
if (pos != std::string::npos) {
std::cout << "Found 'World' at position: " << pos << std::endl;
}
// 提取子串
std::string sub = s1.substr(7, 5);
std::cout << "Substr: " << sub << std::endl;
return 0;
}
3. 比较操作
std::string
支持各种比较操作,包括相等、大小比较等。
#include <iostream>
#include <string>
int main() {
std::string s1 = "Apple";
std::string s2 = "Banana";
if (s1 < s2) {
std::cout << s1 << " is less than " << s2 << std::endl;
} else {
std::cout << s1 << " is not less than " << s2 << std::endl;
}
return 0;
}
4. 输入输出
可以使用流操作符 <<
和 >>
来进行 std::string
的输入和输出操作。
#include <iostream>
#include <string>
int main() {
std::string s;
std::cout << "Enter a string: ";
std::getline(std::cin, s); // 使用 getline 读取整行输入
std::cout << "You entered: " << s << std::endl;
return 0;
}
5. 总结
<string>
提供了对字符串的强大支持,通过 std::string
类可以方便地创建、操作和管理字符串。它封装了字符数组的动态管理,使得字符串操作更为高效和简洁。掌握 std::string
的各种操作和功能,可以显著提升字符串处理的效率和程序的可读性。