2025-01-10 22:29:51 +08:00
|
|
|
|
# string 字符串模板
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
`#include <string>`
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 构造
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string(); // 默认构造,创建一个空的字符串
|
2024-11-23 11:00:35 +00:00
|
|
|
|
string(const char &str1); // c风格字符串初始化
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string(int n,char c); // 用字符填充一个字符串
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 赋值
|
|
|
|
|
```cpp
|
2024-11-23 11:00:35 +00:00
|
|
|
|
string& operator=(const char* str1); // c风格字符串赋值给当前string类
|
|
|
|
|
string& operator=(const string& buf); // 另一个容器复制
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string& operator=(const cahr c); // 字符赋值
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 存取
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
char& operator[](int n); // 通过[]获取字符,注意溢出
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 拼接
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
string& operator+=(const string& str); // 追加到末尾
|
|
|
|
|
string& operator+=(const char* str); // 追加到末尾
|
|
|
|
|
string& operator+=(const char c); // 追加到末尾
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 查找
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
int find(const string& str, int pos = 0) const; // 查找str在当前字符串第一次出现的位置,pos为开始查找的位置
|
|
|
|
|
int find(const char* str, int pos = 0) const; // 查找str在当前字符串第一次出现的位置
|
|
|
|
|
int rfind(const char* str, int pos = npos) const; // 查找str在当前字符串第一次出现的位置,反向查询
|
|
|
|
|
|
|
|
|
|
string::npos 在值上等于-1 即size_t的最大值 表示直到字符串结束
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 替换
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
string& replace(int pos, int n, const string& str); // 从pos开始替换,n个字符
|
|
|
|
|
string& replace(int pos, int n, const char* str); // 从pos开始替换,n个字符
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 比较
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
int compare(const string& str) const; //根据字典序
|
|
|
|
|
int compare(const char* str) const; //根据字典序
|
|
|
|
|
|
|
|
|
|
各种比较操作符都有重载
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 子串
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string substr(int pos = 0, int n = npos) const; //返回从pos开始,长度为n的子串
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 插入
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
string& insert(int pos, const char* str); //在pos位置插入
|
|
|
|
|
string& insert(int pos, const string& str); //在pos位置插入
|
2025-01-10 22:29:51 +08:00
|
|
|
|
string& insert(int pos, int n, char c); //在pos位置插入
|
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 删除
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
|
|
|
|
string& erase(int pos, int n = npos); //在pos位置插入
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 两种转换
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string str1;
|
|
|
|
|
const char* str2 = str1.c_str();
|
|
|
|
|
|
|
|
|
|
const char* str3;
|
|
|
|
|
string str4(str3);
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|
2024-10-27 09:23:50 +00:00
|
|
|
|
|
2025-01-10 22:29:51 +08:00
|
|
|
|
## 神奇转换函数
|
|
|
|
|
```cpp
|
2024-10-27 09:23:50 +00:00
|
|
|
|
string to_ strinf(任何类型); //将好多类型转换成string
|
2025-01-10 22:29:51 +08:00
|
|
|
|
```
|