This commit is contained in:
e2hang
2025-08-01 00:22:15 +08:00
parent d510f4c75b
commit 11fd55f825
8 changed files with 227 additions and 0 deletions

54
STL/STL-List/main.cpp Normal file
View File

@@ -0,0 +1,54 @@
#include <iostream>
#include <list>
using namespace std;
int main(){
list<string> a;
a.push_back("1.Ohyeah");
a.push_back("2.Ohno");
std::list<string>::iterator it = a.begin();
auto ite = a.cbegin();
cout << *ite << endl;
a.insert(++it, "0.Ohoh");
a.push_back("3.Ohhoho");
for(string x : a){
cout << x << " ";
}
cout << endl;
//Copy a to b
list<string> b(a);
auto itmp1 = a.begin();
itmp1++;
auto itmp2 = a.end();
itmp2--;
a.erase(itmp1, itmp2);
for(string x : a){
cout << x << " ";
}
cout << endl << endl;
//Two Methods going through The Whole List
auto iit = a.begin();
for(int i = 0; i < a.size(); i++){
cout << *iit << " ";
iit++;
}
cout << endl;
cout << endl;
//insert ?
for(string x : b){
cout << x << " ";
}
cout << endl;
auto itb = b.begin();
itb++;itb++;
b.insert(itb, a.begin(), a.end()); //insert into pos 2
for(string x : b){
cout << x << " ";
}
cout << endl;
return 0;
}

BIN
STL/STL-List/main.exe Normal file

Binary file not shown.

175
STL/STL-String/README.md Normal file
View File

@@ -0,0 +1,175 @@
`std::string` 是 C++ 标准模板库STL中的一个非常重要的类用于处理文本字符串。它是 `std::basic_string<char>` 的 typedef定义在头文件 `<string>` 中。
下面我为你系统总结 `std::string` 的所有常用用法和函数,按类别分类,力求全面:
---
### 🔹 1. **构造函数**
```cpp
std::string s1; // 空字符串
std::string s2("hello"); // 从 C 字符串构造
std::string s3(s2); // 拷贝构造
std::string s4(s2, 1, 3); // 从 s2 的第1个字符起拷贝3个字符 -> "ell"
std::string s5(5, 'x'); // 重复字符 -> "xxxxx"
std::string s6 = {'a','b','c'}; // 用 initializer_list
```
---
### 🔹 2. **赋值和操作符**
```cpp
s1 = "world";
s2 = s1;
s3 = std::move(s1); // 移动赋值
s1 += "!"; // 拼接
std::string s = s2 + s3;
```
---
### 🔹 3. **基本成员函数**
```cpp
s.size(); // 返回长度
s.length(); // 同 size()
s.empty(); // 是否为空
s.clear(); // 清空
s.capacity(); // 当前容量
s.reserve(100); // 预留容量
s.shrink_to_fit(); // 收缩容量
```
---
### 🔹 4. **访问字符**
```cpp
s[i]; // 下标访问(不检查越界)
s.at(i); // 带越界检查
s.front(); // 第一个字符
s.back(); // 最后一个字符
```
---
### 🔹 5. **修改字符串**
```cpp
s.insert(pos, "abc"); // 插入字符串
s.insert(pos, 5, 'x'); // 插入5个x
s.erase(pos, len); // 删除从 pos 起 len 个字符
s.replace(pos, len, "newstr"); // 替换部分内容
s.push_back('c'); // 追加一个字符
s.pop_back(); // 移除最后一个字符
s.append("extra"); // 追加字符串
s.swap(s2); // 交换两个字符串
```
---
### 🔹 6. **查找字符串**
```cpp
s.find("abc"); // 查找子串,返回位置或 string::npos
s.find("abc", pos); // 从指定位置起查找
s.rfind("abc"); // 反向查找
s.find_first_of("abc"); // 查找任一字符
s.find_last_of("abc"); // 查找任一字符(从后往前)
s.find_first_not_of(" \n"); // 找第一个不是空格或换行的字符
```
---
### 🔹 7. **子串**
```cpp
s.substr(pos); // 从 pos 到末尾的子串
s.substr(pos, len); // 从 pos 起 len 个字符
```
---
### 🔹 8. **比较字符串**
```cpp
s1 == s2;
s1 != s2;
s1 < s2;
s1.compare(s2); // 返回 <0, =0, >0
s1.compare(pos, len, s2);
```
---
### 🔹 9. **C 风格字符串**
```cpp
s.c_str(); // 返回 const char* 指针
s.data(); // 类似 c_str(),但 C++17 起返回非 null 终止的指针
```
---
### 🔹 10. **迭代器**
```cpp
s.begin(); s.end(); // 正向迭代器
s.rbegin(); s.rend(); // 反向迭代器
const_iterator it = s.cbegin(); // 常量迭代器
```
---
### 🔹 11. **输入输出**
```cpp
std::cin >> s; // 读入单词
std::getline(std::cin, s); // 读入整行(包含空格)
std::cout << s; // 输出字符串
```
---
### 🔹 12. **其他函数**
```cpp
s.resize(10); // 改变字符串长度
s.resize(10, 'x'); // 用'x'填充
std::hash<std::string>{}(s); // 获取哈希值
```
---
### 🔹 13. **C++20 增强**
在 C++20 中,`std::string` 支持了 Ranges、`starts_with()``ends_with()` 等:
```cpp
s.starts_with("http"); // 是否以 http 开头
s.ends_with(".com"); // 是否以 .com 结尾
```
---
### 🔹 14. **和 C 字符串兼容**
```cpp
const char* cstr = s.c_str(); // 可用于 C 函数如 printf("%s", s.c_str())
std::string(some_c_str); // 从 C 字符串构造
```
---
### 🔹 15. **字符串与数字转换**
```cpp
int i = std::stoi("123");
double d = std::stod("3.14");
std::string s = std::to_string(42);
```
---
如需我帮你整理成一份笔记或 Markdown 文件,也可以告诉我!你也可以提你最常用的用法,我帮你列举示例。

52
STL/STL-String/string.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <string>
using namespace std;
int main(){
string a;
a = "Good Game!";
cout << a << endl;
string::iterator it = a.begin();
a.insert(5, "Fucking ");
cout << a << endl;
cout << a.length() << endl;
cout << a.front() << " " << a.back() << endl;
a.erase(0, 5);
cout << a << endl;
a.append(" That is good!");
a += "test";
cout << a << endl;
a.pop_back();a.pop_back();
cout << a << endl;
a.pop_back();a.pop_back();
cout << a << endl;
a.replace(8, 12, "Gay");
cout << a << endl;
cout << a.find("Gays") << endl;
string b = a.substr(8, 3);
cout << b << endl;
b.insert(2, 2, 'H');
cout << b << endl;
int i = std::stoi("123");
double d = std::stod("3.14");
string s = std::to_string(42);
cout << s << " " ;
s.append("Orz");
cout << s << endl;
return 0;
}
/*
Good Game!
Good Fucking Game!
18
G !
Fucking Game!
Fucking Game! That is good!test
Fucking Game! That is good!te
Fucking Game! That is good!
Fucking Gays good!
8
Gay
GaHHy
42 42Orz
*/

BIN
STL/STL-String/string.exe Normal file

Binary file not shown.

31
STL/STL-Vector/define.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> a;
vector<int> b(3,100);//b = {100, 100, 100}
vector<int> c(b.begin(),b.end());
vector<int> d(c);
for(/*vector<int>::iterator*/ auto ti = b.begin(); ti != b.end(); ti++){
cout << *ti << " ";
}
cout << endl;
b.push_back(300);
b.push_back(500);
cout << b.size() << " " << b.capacity() << endl;
b.resize(10);
b.reserve(100);
cout << b.size() << " " << b.capacity() << endl;
auto ti = b.begin() + 3;
b.insert(ti, 20);
for(int i = 0; i < b.size(); i++){
cout << b.at(i) << " ";
}
cout << endl;
return 0;
}

BIN
STL/STL-Vector/define.exe Normal file

Binary file not shown.

48
STL/STL-Vector/vector.md Normal file
View File

@@ -0,0 +1,48 @@
Member functions
(constructor) Construct vector (public member function)
(destructor) Vector destructor (public member function)
operator= Assign content (public member function)
Iterators:
begin Return iterator to beginning (public member function)
end Return iterator to end (public member function)
rbegin Return reverse iterator to reverse beginning (public member function)
rend Return reverse iterator to reverse end (public member function)
cbegin Return const_iterator to beginning (public member function)
cend Return const_iterator to end (public member function)
crbegin Return const_reverse_iterator to reverse beginning (public member function)
crend Return const_reverse_iterator to reverse end (public member function)
Capacity:
size Return size (public member function)
max_size Return maximum size (public member function)
resize Change size (public member function)
capacity Return size of allocated storage capacity (public member function)
empty Test whether vector is empty (public member function)
reserve Request a change in capacity (public member function)
shrink_to_fit Shrink to fit (public member function)
Element access:
operator[] Access element (public member function)
at Access element (public member function)
front Access first element (public member function)
back Access last element (public member function)
data Access data (public member function)
Modifiers:
assign Assign vector content (public member function)
push_back Add element at the end (public member function)
pop_back Delete last element (public member function)
insert Insert elements (public member function)
erase Erase elements (public member function)
swap Swap content (public member function)
clear Clear content (public member function)
emplace Construct and insert element (public member function)
emplace_back Construct and insert element at the end (public member function)
Allocator:
get_allocator Get allocator (public member function)
Non-member function overloads
relational operators Relational operators for vector (function template)
swap Exchange contents of vectors (function template)