C++ 中的 std::string 标准说明
概述
std::string 是 C++ 标准库中用于处理字符串的类,位于 <string> 头文件中。它属于 std 命名空间,提供了强大的功能来操作和管理字符序列。与 C 风格字符串(以 \0 结尾的字符数组)相比,std::string 更加安全、灵活且易于使用。
包含头文件
在使用 std::string 之前,需要包含以下头文件:
#include <string>
基本特性
- 动态内存管理:
std::string自动管理其内部存储,无需手动分配或释放内存。 - 安全性:避免了缓冲区溢出等问题,因为
std::string总是知道其长度。 - 丰富的接口:提供了多种成员函数和操作符重载,方便进行字符串的创建、修改、查找和比较等操作。
构造方式
std::string 提供了多种构造方法:
| 构造方法 | 描述 |
|---|---|
std::string() |
创建一个空字符串。 |
std::string(const char* s) |
使用 C 风格字符串初始化。 |
std::string(const string& str) |
拷贝构造函数。 |
std::string(size_t n, char c) |
创建一个包含 n 个字符 c 的字符串。 |
std::string(const string& str, size_t pos, size_t len = npos) |
从字符串 str 的第 pos 位开始复制最多 len 个字符。 |
示例:
cpp 复制代码std::string s1; // 空字符串
std::string s2("hello"); // 初始化为 "hello"
std::string s3(s2); // 拷贝构造
std::string s4(5, 'a'); // 初始化为 "aaaaa"
std::string s5(s2, 1, 3); // 初始化为 "ell" (从 s2 的第 1 位开始取 3 个字符)
成员函数
1. 字符串访问
| 函数 | 描述 |
|---|---|
size() 或 length() |
返回字符串的长度。 |
empty() |
检查字符串是否为空。 |
at(size_t pos) |
返回指定位置的字符(范围检查)。 |
operator[] |
返回指定位置的字符(不检查范围)。 |
front() |
返回第一个字符。 |
back() |
返回最后一个字符。 |
示例:
cpp 复制代码std::string s = "hello";
std::cout << s.size() << std::endl; // 输出 5
std::cout << s.empty() << std::endl; // 输出 0 (false)
std::cout << s.at(1) << std::endl; // 输出 'e'
std::cout << s[1] << std::endl; // 输出 'e'
std::cout << s.front() << std::endl; // 输出 'h'
std::cout << s.back() << std::endl; // 输出 'o'
2. 修改字符串
| 函数 | 描述 |
|---|---|
clear() |
清空字符串内容。 |
resize(size_t n, char c = char()) |
调整字符串大小,不足部分用 c 填充。 |
append(const string& str) |
追加另一个字符串。 |
insert(size_t pos, const string& str) |
在指定位置插入字符串。 |
erase(size_t pos, size_t len = npos) |
删除从 pos 开始的 len 个字符。 |
replace(size_t pos, size_t len, const string& str) |
替换从 pos 开始的 len 个字符为 str。 |
示例:
cpp 复制代码std::string s = "hello";
s.append(" world"); // "hello world"
s.insert(5, ", "); // "hello, world"
s.erase(5, 2); // "hello world"
s.replace(6, 5, "planet"); // "hello planet"
s.clear(); // ""
3. 查找与比较
| 函数 | 描述 |
|---|---|
find(const string& str, size_t pos = 0) |
查找子字符串首次出现的位置。 |
rfind(const string& str, size_t pos = npos) |
查找子字符串最后一次出现的位置。 |
compare(const string& str) |
比较两个字符串(返回值为负数、零或正数)。 |
示例:
cpp 复制代码std::string s = "hello world";
size_t pos = s.find("world"); // 返回 6
pos = s.rfind("l"); // 返回 9
int cmp = s.compare("hello"); // 返回正值
4. 子字符串操作
| 函数 | 描述 |
|---|---|
substr(size_t pos, size_t len = npos) |
提取从 pos 开始的 len 个字符。 |
示例:
cpp 复制代码std::string s = "hello world";
std::string sub = s.substr(0, 5); // "hello"
运算符重载
std::string 支持多种运算符重载,便于字符串操作:
+和+=:连接字符串。==和!=:比较字符串是否相等。<,<=,>,>=:按字典顺序比较字符串。[]:访问字符串中的单个字符。
示例:
cpp 复制代码std::string s1 = "hello";
std::string s2 = " world";
std::string s3 = s1 + s2; // "hello world"
bool isEqual = (s1 == "hello"); // true
性能注意事项
- 小字符串优化(SSO):许多现代 C++ 实现对短字符串进行了优化,将它们存储在栈上而不是堆上,从而减少内存分配开销。
- 拷贝控制:
std::string实现了高效的拷贝控制机制(如移动语义),但频繁的拷贝仍可能导致性能问题。
示例代码
以下是一个完整的示例,展示了 std::string 的基本用法:
#include <iostream>
#include <string>
int fun() {
std::string s1 = "hello";
std::string s2 = "world";
// 连接字符串
std::string s3 = s1 + " " + s2;
std::cout << "Concatenated: " << s3 << std::endl;
// 查找子字符串
size_t pos = s3.find("world");
if (pos != std::string::npos) {
std::cout << "Found 'world' at position: " << pos << std::endl;
}
// 替换子字符串
s3.replace(pos, 5, "planet");
std::cout << "Replaced: " << s3 << std::endl;
return 0;
}
来源
基于 C++ 标准库的相关知识编写。更多详细信息可参考 C++ 官方文档。
如果您有任何疑问或需要进一步的帮助,请随时联系我们!
需要确认型号或具体项目?联系技术支持 ↗