material/c++/universal/string.txt

93 lines
2.7 KiB
Plaintext
Raw Normal View History

2024-10-27 09:23:50 +00:00
# c风格字符串处理函数
strcpy(buf, "hello"); // 用后面的字符串覆盖前面的字符串
strncpy(buf, "hello", 5); // 增加指定长度
strcat(buf, "hello"); // 连接字符串
strncat(buf, "hello", 5);
strcmp(buf, "hello"); // 比较字符串,相等返回0
strlen(buf); // 返回长度,不包括'\0'
strstr(buf, "hello"); // 返回第一次出现子串的指针
strtok(buf, " "); // 分割字符串,返回分割过的字符串
memset(buf, 0, sizeof(buf));//覆写内存块
memcpy(buf, "hello", 5); //复制内存块
memmove(buf + 1, buf, 4); //移动5个字节到前一个位置
# string
## 构造
string(); // 默认构造,创建一个空的字符串
string(const char &str); // c风格字符串初始化
string(int n,char c); // 用字符填充一个字符串
## 赋值
string& operator=(const char* str); // c风格字符串赋值给当前string类
string& operator=(const string& str); // 另一个容器复制
string& operator=(const cahr c); // 字符赋值
## 存取
char& operator[](int n); // 通过[]获取字符,注意溢出
## 拼接
string& operator+=(const string& str); // 追加到末尾
string& operator+=(const char* str); // 追加到末尾
string& operator+=(const char c); // 追加到末尾
## 查找
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的最大值 表示直到字符串结束
## 替换
string& replace(int pos, int n, const string& str); // 从pos开始替换n个字符
string& replace(int pos, int n, const char* str); // 从pos开始替换n个字符
## 比较
int compare(const string& str) const; //根据字典序
int compare(const char* str) const; //根据字典序
各种比较操作符都有重载
## 子串
string substr(int pos = 0, int n = npos) const; //返回从pos开始,长度为n的子串
## 插入
string& insert(int pos, const char* str); //在pos位置插入
string& insert(int pos, const string& str); //在pos位置插入
string& insert(int pos, int n, char c); //在pos位置插入
## 删除
string& erase(int pos, int n = npos); //在pos位置插入
# 两种转换
string str1;
const char* str2 = str1.c_str();
const char* str3;
string str4(str3);
# 神奇转换函数
string to_ strinf(任何类型); //将好多类型转换成string