
一.string的基本概念
本质
string是C++风格的字符串,而string本质是一个字符串
string和char * 区别
- char * 是一个指针
- string是一个类,类内部封装了char *,管理这个字符串,是一个char * 型容器。
特点
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
二.string构造函数
以下是std::string的构造函数原型整理成表格的形式:
| 构造函数 | 描述 | 
|---|---|
| string() | 默认构造函数 | 
| string(const char* s) | 使用C风格字符串构造函数 | 
| string(const char* s, size_t n) | 使用C风格字符串和长度构造函数 | 
| string(size_t n, char c) | 使用重复字符构造函数 | 
| string(const string& str) | 拷贝构造函数 | 
| string(string&& str) noexcept | 移动构造函数 | 
| template string(InputIterator first, InputIterator last) | 使用迭代器范围构造函数 | 
| string(initializer_list ilist) | 使用初始化列表构造函数 | 
#include <iostream>
#include <string>
using namespace std;
int main() {
	//默认构造函数
	string str1;
	string str2("使用C风格字符串构造函数");
	string str3("使用C风格字符串和长度构造函数", 5);
	string str4(10, 'a');
	string str = "拷贝构造函数";
	string str5(str);
	string&& str6 = move("移动构造函数");
	str = "使用迭代器范围构造函数";
	string str7(str.begin(), str.end());
	string str8[2] = { "使用初始化", "列表构造函数" };
	cout << "wetr";
	cout << str1 << endl;
	cout << str2 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	cout << str5 << endl;
	cout << str6 << endl;
	cout << str7 << endl;
	cout << str8[1]<< str8[2] << endl;
	return 0;
}
![2023-09-01T01:46:05.png][1]
二.string的赋值操作
以下是std::string的赋值操作函数原型整理成表格的形式:
| 函数原型 | 描述 | 
|---|---|
| string& operator=(const string& str) | 赋值运算符重载:从另一个字符串赋值 | 
| string& operator=(string&& str) noexcept | 赋值运算符重载:移动赋值 | 
| string& operator=(const char* s) | 赋值运算符重载:从C风格字符串赋值 | 
| string& operator=(char c) | 赋值运算符重载:从单个字符赋值 | 
| string& operator=(initializer_list<char> ilist) | 赋值运算符重载:从初始化列表赋值 | 
| string& assign(const string& str) | 成员函数:从另一个字符串赋值 | 
| string& assign(string&& str) noexcept | 成员函数:移动赋值 | 
| string& assign(const char* s) | 成员函数:从C风格字符串赋值 | 
| string& assign(const char* s, size_t n) | 成员函数:从C风格字符串和长度赋值 | 
| string& assign(size_t n, char c) | 成员函数:使用重复字符赋值 | 
| string& assign(initializer_list<char> ilist) | 成员函数:从初始化列表赋值 | 
| string& assign(const_iterator first, const_iterator last) | 成员函数:使用迭代器范围赋值 | 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	str1 = "从另一个字符串赋值";
	cout << str1 << endl;
	str1 = move("移动赋值");
	cout << str1 << endl;
	str1 = "从C风格字符串赋值";
	cout << str1 << endl;
	string str = "  使用迭代器范围赋值";
	str1.assign(str.begin()+2, str.end());
	cout << str1 << endl;
	return 0;
}
![2023-09-01T02:02:34.png][2]
三.string拼接操作
以下是std::string的字符串拼接操作函数原型整理成表格的形式:
| 函数原型 | 描述 | 
|---|---|
| string operator+(const string& lhs, const string& rhs) | 运算符重载:字符串与字符串的拼接 | 
| string operator+(const string& lhs, const char* rhs) | 运算符重载:字符串与C风格字符串的拼接 | 
| string operator+(const char* lhs, const string& rhs) | 运算符重载:C风格字符串与字符串的拼接 | 
| string operator+(const string& lhs, char rhs) | 运算符重载:字符串与单个字符的拼接 | 
| string operator+(char lhs, const string& rhs) | 运算符重载:单个字符与字符串的拼接 | 
| string& append(const string& str) | 成员函数:追加拼接另一个字符串 | 
| string& append(const string& str, size_t subpos, size_t sublen) | 成员函数:从另一个字符串的指定位置截取指定长度后进行拼接 | 
| string& append(const char* s) | 成员函数:追加拼接C风格字符串 | 
| string& append(const char* s, size_t n) | 成员函数:追加拼接C风格字符串的指定长度 | 
| string& append(size_t n, char c) | 成员函数:追加拼接指定数量的重复字符 | 
| string& append(initializer_list<char> ilist) | 成员函数:追加拼接初始化列表中的字符 | 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str;
	string str1 = "01234567";
	str += "hello";
	str += string("word");
	str += (string("  word") + "20  ");
	str.append(str1, 5, 2);
	cout << str;
	return 0;
}
四.string的查找和替换
下面是关于std::string查找和替换的常用函数原型:
查找相关函数:
| 函数原型 | 描述 | 
|---|---|
| size_t find(const string& str, size_t pos = 0) const noexcept; | 在字符串中查找另一个字符串的位置 | 
| size_t find(const char* s, size_t pos = 0) const; | 在字符串中查找C风格字符串的位置 | 
| size_t find(char c, size_t pos = 0) const noexcept; | 在字符串中查找字符的位置 | 
| size_t rfind(const string& str, size_t pos = npos) const noexcept; | 在字符串中从后往前查找另一个字符串的位置 | 
| size_t rfind(const char* s, size_t pos = npos) const; | 在字符串中从后往前查找C风格字符串的位置 | 
| size_t rfind(char c, size_t pos = npos) const noexcept; | 在字符串中从后往前查找字符的位置 | 
| size_t find_first_of(const string& str, size_t pos = 0) const noexcept; | 在字符串中查找第一个匹配另一个字符串中任意字符的位置 | 
| size_t find_first_of(const char* s, size_t pos = 0) const; | 在字符串中查找第一个匹配C风格字符串中任意字符的位置 | 
| size_t find_first_of(char c, size_t pos = 0) const noexcept; | 在字符串中查找第一个匹配字符的位置 | 
| size_t find_last_of(const string& str, size_t pos = npos) const noexcept; | 在字符串中从后往前查找第一个匹配另一个字符串中任意字符的位置 | 
| size_t find_last_of(const char* s, size_t pos = npos) const; | 在字符串中从后往前查找第一个匹配C风格字符串中任意字符的位置 | 
| size_t find_last_of(char c, size_t pos = npos) const noexcept; | 在字符串中从后往前查找第一个匹配字符的位置 | 
| size_t find_first_not_of(const string& str, size_t pos = 0) const noexcept; | 在字符串中查找第一个不匹配另一个字符串中任意字符的位置 | 
| size_t find_first_not_of(const char* s, size_t pos = 0) const; | 在字符串中查找第一个不匹配C风格字符串中任意字符的位置 | 
| size_t find_first_not_of(char c, size_t pos = 0) const noexcept; | 在字符串中查找第一个不匹配字符的位置 | 
| size_t find_last_not_of(const string& str, size_t pos = npos) const noexcept; | 在字符串中从后往前查找第一个不匹配另一个字符串中任意字符的位置 | 
| size_t find_last_not_of(const char* s, size_t pos = npos) const; | 在字符串中从后往前查找第一个不匹配C风格字符串中任意字符的位置 | 
| size_t find_last_not_of(char c, size_t pos = npos) const noexcept; | 在字符串中从后往前查找第一个不匹配字符的位置 | 
替换相关函数:
| 函数原型 | 描述 | 
|---|---|
| string& replace(size_t pos, size_t len, const string& str); | 替换指定位置和长度的子字符串为另一个字符串 | 
| string& replace(size_t pos, size_t len, const char* s); | 替换指定位置和长度的子字符串为C风格字符串 | 
| string& replace(size_t pos, size_t len, const char* s, size_t n); | 替换指定位置和长度的子字符串为C风格字符串的指定长度 | 
| string& replace(size_t pos, size_t len, size_t n, char c); | 替换指定位置和长度的子字符串为指定数量的重复字符 | 
| string& replace(iterator first, iterator last, const string& str); | 替换迭代器范围内的子字符串为另一个字符串 | 
| string& replace(iterator first, iterator last, const char* s); | 替换迭代器范围内的子字符串为C风格字符串 | 
| string& replace(iterator first, iterator last, const char* s, size_t n); | 替换迭代器范围内的子字符串为C风格字符串的指定长度 | 
| string& replace(iterator first, iterator last, size_t n, char c); | 替换迭代器范围内的子字符串为指定数量的重复字符 | 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str = "1234578912345789";
	int a = str.find("78");
	cout << a << endl;
	a = str.rfind("78");
	cout << a << endl;
	//从下标1开始的三个字符替换成abcd
	str.replace(1, 3, "abcd");
	cout << str << endl;
	return 0;
}
![2023-09-01T02:19:00.png][3]
五.string的字符串比较
下面是关于std::string比较的常用函数原型:
| 函数原型 | 描述 | 
|---|---|
| int compare(const string& str) const noexcept; | 比较字符串和另一个字符串 | 
| int compare(size_t pos, size_t len, const string& str) const; | 比较字符串的子串和另一个字符串 | 
| int compare(const char* s) const; | 比较字符串和C风格字符串 | 
| int compare(size_t pos, size_t len, const char* s) const; | 比较字符串的子串和C风格字符串 | 
| int compare(size_t pos, size_t len, const char* s, size_t n) const; | 比较字符串的子串和指定长度的C风格字符串 | 
上述函数通过返回整数值来表示比较的结果,其返回值的含义如下:
-  返回值 < 0 表示当前字符串小于目标字符串。 
-  返回值 > 0 表示当前字符串大于目标字符串。 
-  返回值 = 0 表示当前字符串等于目标字符串。 #include 
 #include
 using namespace std;
 int main() {
 string str = “1234578912345789”;
 //从0下标开始的6个字符的子串,后一个的字串比较
 int m=str.compare(0,6, “12345789”, 6);
 cout << m << endl;
 return 0;
 }
六.string字符串存取
下面是关于std::string字符串存取的常用函数原型:
| 函数原型 | 描述 | 
|---|---|
| char& operator[](size_t pos); | 访问字符串中指定位置的字符 | 
| const char& operator[](size_t pos) const; | 访问字符串中指定位置的字符(const版本) | 
| char& at(size_t pos); | 访问字符串中指定位置的字符,带范围检查 | 
| const char& at(size_t pos) const; | 访问字符串中指定位置的字符,带范围检查(const版本) | 
| char& front(); | 访问字符串的第一个字符 | 
| const char& front() const; | 访问字符串的第一个字符(const版本) | 
| char& back(); | 访问字符串的最后一个字符 | 
| const char& back() const; | 访问字符串的最后一个字符(const版本) | 
| const char* data() const noexcept; | 返回字符串数据的指针 | 
| const char* c_str() const noexcept; | 返回以空字符结尾的字符串的指针(C风格字符串) | 
| const char* data() const noexcept; | 返回字符串数据的指针 | 
| const char* c_str() const noexcept; | 返回以空字符结尾的字符串的指针(C风格字符串) | 
这些函数允许您对std::string对象进行字符的存取操作,可以直接访问特定位置的字符,或者获取字符串的首尾字符。data()和c_str()函数还可以返回字符串数据的指针(以及以空字符结尾的C风格字符串的指针),方便与C语言接口进行交互操作。请注意,在只读的场景下使用at()函数可以提供范围检查,避免越界访问。
七.string的插入和删除
下面是关于std::string插入和删除字符串的常用函数原型:
| 函数原型 | 描述 | 
|---|---|
| string& insert(size_t pos, const string& str); | 在指定位置插入另一个字符串 | 
| string& insert(size_t pos, const string& str, size_t subpos, size_t sublen); | 在指定位置插入另一个字符串的子串 | 
| string& insert(size_t pos, const char* s); | 在指定位置插入C风格字符串 | 
| string& insert(size_t pos, const char* s, size_t n); | 在指定位置插入指定长度的C风格字符串 | 
| string& insert(size_t pos, size_t n, char c); | 在指定位置插入指定数量的字符 | 
| iterator insert(iterator p, char c); | 在指定位置插入单个字符 | 
| iterator insert(iterator p, size_t n, char c); | 在指定位置插入指定数量的字符 | 
| iterator insert(iterator p, InputIt first, InputIt last); | 在指定位置插入迭代器范围的字符 | 
| template <class InputIt> void insert(iterator p, InputIt first, InputIt last); | 在指定位置插入迭代器范围的字符 | 
| template <class InputIt> void insert(iterator p, initializer_list<typename InputIt::value_type> ilist); | 在指定位置插入初始化列表的字符 | 
| string& erase(size_t pos = 0, size_t len = npos); | 删除字符串的子串 | 
| iterator erase(iterator p); | 删除指定位置的字符 | 
| iterator erase(iterator first, iterator last); | 删除迭代器范围内的字符 | 
这些插入和删除函数允许您在
std::string对象中插入或删除字符串内容。通过这些函数,您可以在指定位置插入字符串、字符或字符范围,也可以删除字符串子串、指定位置的字符或一段字符范围。这些函数提供了灵活的方式来操作字符串,满足不同场景下的需求。
八.获得string的子串
以下是使用substr函数获取字符串子串的表格总结:
| 函数原型 | 描述 | 
|---|---|
| string substr(size_t pos = 0, size_t len = npos) const; | 获取从指定位置开始的一段子串 | 
示例:
std::string str = "Hello, World!";
std::string sub1 = str.substr(7);        // "World!"
std::string sub2 = str.substr(0, 5);     // "Hello"
std::string sub3 = str.substr(7, 5);     // "World"
使用substr函数,您可以方便地获得std::string的子串,通过指定起始位置和长度参数来进行操作。
九.函数接口
| 序号 | 函数接口 | 功能描述 | 
|---|---|---|
| 1 | push_back© | 在字符串的末尾添加字符 c | 
| 2 | pop_back() | 删除字符串的最后一个字符 | 
| 3 | resize(n) | 调整字符串的大小为 n | 
| 4 | resize(n, c) | 调整字符串的大小为 n,并将新增的元素初始化为字符 c | 
| 5 | front() | 返回字符串的第一个字符 | 
| 6 | back() | 返回字符串的最后一个字符 | 
| 7 | find_first_of(str) | 在字符串中查找字符串 str 中的任意字符第一次出现的位置 | 
| 8 | find_first_not_of(str) | 在字符串中查找字符串 str 中的任意字符第一次不出现的位置 | 
| 9 | find_last_of(str) | 在字符串中从后往前查找字符串 str 中的任意字符最后一次出现的位置 | 
| 10 | find_last_not_of(str) | 在字符串中从后往前查找字符串 str 中的任意字符最后一次不出现的位置 | 
| 11 | toLowerCase() | 将字符串中的字母全部转换为小写 | 
| 12 | toUpperCase() | 将字符串中的字母全部转换为大写 | 
| 13 | at(n) | 返回字符串中索引为 n 的字符 | 
| 14 | operator[] (n) | 返回字符串中索引为 n 的字符 | 
| 15 | substr(pos) | 返回从位置 pos 开始到字符串末尾的子字符串 | 
| 16 | compare(pos, len, str) | 将字符串中从位置 pos 开始的长度为 len 的子字符串与字符串 str 比较 | 
| 17 | getline(input_stream, str, delimiter) | 从输入流 input_stream 中读取一行文本,并将结果存储到字符串 str 中 | 
| 18 | stoi(str) | 将字符串 str 转换为整数值 | 
| 19 | stol(str) | 将字符串 str 转换为长整数值 | 
| 20 | stof(str) | 将字符串 str 转换为浮点数值 | 
| 21 | to_string(value) | 将值 value 转换为字符串 | 
| 22 | find_last_of(str, pos) | 在字符串中从位置 pos 开始从后往前查找字符串 str 第一次出现的位置 | 
| 23 | find_last_not_of(str, pos) | 在字符串中从位置 pos 开始从后往前查找字符串 str 第一次不出现的位置 | 
| 24 | replace(pos, len, newstr, newlen) | 将字符串中从位置 pos 开始的长度为 len 的子字符串替换为新的字符串 | 
| 25 | find_first_of(str, pos) | 在字符串中从位置 pos 开始查找字符串 str 中的任意字符第一次出现的位置 | 
| 26 | find_first_not_of(str, pos) | 在字符串中从位置 pos 开始查找字符串 str 中的任意字符第一次不出现的位置 | 
| 27 | compare(pos, len, str, subpos, sublen) | 将字符串中从位置 pos 开始的长度为 len 的子字符串与字符串 str 中从位置 subpos 开始的长度为 sublen 的子字符串进行比较 | 
| 28 | replace(pos, len, str) | 将字符串中从位置 pos 开始的长度为 len 的子字符串替换为字符串 str | 
| 29 | capacity() | 返回字符串当前能够容纳的字符数 | 
| 30 | reserve(n) | 将字符串的容量设置为至少为 n 个字符 | 
| 31 | shrink_to_fit() | 将字符串的容量调整为与字符串的长度相匹配,以节省内存空间 | 
| 32 | find_last_of(str, pos) | 在字符串中从位置 pos 开始从后往前查找字符串 str 中的任意字符最后一次出现的位置 | 
| 33 | find_last_not_of(str, pos) | 在字符串中从位置 pos 开始从后往前查找字符串 str 中的任意字符最后一次不出现的位置 | 
| 34 | replace(pos, len, newstr) | 将字符串中从位置 pos 开始的长度为 len 的子字符串替换为新的字符串 | 
| 35 | replace(iterator1, iterator2, newstr) | 将迭代器 iterator1 和 iterator2 之间的子字符串替换为新的字符串 | 
| 36 | copy(char_array, len, pos) | 将字符串中从位置 pos 开始的长度为 len 的子字符串复制到字符数组 char_array 中 | 
| 37 | find_if(predicate) | 在字符串中查找满足谓词函数 predicate 的第一个字符的位置 | 
| 38 | rfind(str) | 在字符串中从后往前查找子字符串 str 并返回第一次出现的位置 | 
| 39 | rfind(str, pos) | 在字符串中从位置 pos 开始从后往前查找子字符串 str 并返回第一次出现的位置 | 
| 40 | replace_if(predicate, newstr) | 将字符串中满足谓词函数 predicate 的字符替换为新的字符串 | 
请注意,这里的表格仅提供了功能描述的简要介绍,具体的函数行为和参数要求请查阅官方文档以获取详细信息。

















![element UI table横向树结合checkbox进行多选,实现各个节点的[全选,半选,不选]状态附带模拟数据](https://img-blog.csdnimg.cn/709a4433e1fe492c9e22d7735c0d63b4.png)
