This commit is contained in:
yinkanglong_lab
2021-03-05 21:55:33 +08:00
parent 7ffe1cdd82
commit 1be8d28fc4
51 changed files with 583 additions and 0 deletions

100
C++/标准库/0 简介.md Normal file
View File

@@ -0,0 +1,100 @@
> 参考文献
> * 《C++标准库》
> * 《C++Primer》
> * 《C++ STL使用例子大全》
## 1 C++ 语言新特性
### nullptr
* 取代NULL。用于防止0指针出现的歧义
### auto自动完成类型推导
### 一致性初始化与初值列表
* 可以使用大括号,完成所有的初始化。
### 范围for循环
### constexpr常量表达式
### Template特性
### Lambda表达式
* 没有参数和返回值的lambda表达式
```C++
auto l=[]{
cout<<"hello world"<<endl;
}
```
* lambda表达式的定义完成后直接调用
```C++
[]{ cout<<"helloworld"<<endl;}();
```
* 有参数的lambda表达式
```
auto l = [](int a,int b){cout<<a+b<<endl;};
```
* 有返回值的lambda表达式
```
auto l = [](int m,int n)->int{cout<<m+n<<endl;return m+n;};
```
### decltype关键字
* 让编译器找出表达式的类型。
```
decltype(x+y)
```
### 新的函数声明方法
* 使用参数列表后的箭头声明返回值的类型。与lambda表达式一致。
```
template<typename T1,typename T2>
decltype(x+y) add(T1 x,T2 y);
template<typename T1,typename T2>
auto add(T1 x,T2 y)->decltype(x+y);
```
### 新的基础类型
* char16_t,char32_t
* long long usigned long long
* std::nullptr_t
## 2 一般概念
### 命名空间
使用方法
* 直接指定标识符
```
std::cout<<10<<endl;
```
* 使用using declaration声明
```
using std::cout;
using std::endl;
cout<<10<<endl;
```
* 使用using directive。复杂的代码中容易导致冲突
```
using namespace std;
cout<<hex<<3.4<<endl;
```
### 头文件
### 错误和异常处理
* 标准错误和异常如下:
![](2021-03-05-17-30-07.png)

10
C++/标准库/0.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include<iostream>
using namespace std;
int main(){
int a=1,b=2;
auto l = [](int m,int n)->int{cout<<m+n<<endl;return m+n;};
//省去了函数的名字,生成一个函数对象。可以调用函数。
cout<<l(a,b)<<endl;
return 0;
}

View File

@@ -0,0 +1,3 @@
# 通用工具

90
C++/标准库/2 容器.md Normal file
View File

@@ -0,0 +1,90 @@
# 容器
## 1 简介
> 新标准库的容器壁使用原始的数组实现的数据结构要快很多。经过了精心的优化。
### 确定使用哪种容器
1. 除非有明确的理由否则使用vector
2. 随机元素访问vector或deque
3. 容器中间插入或者插入元素list、forward_list
4. 头尾插入元素使用deque
5. 可以在输入阶段随机插入的时候使用list然后将复制好的放到vector中加速访问。
## 2 容器的基础操作
### 容器统一的操作
![](2021-03-05-20-17-45.png)
![](2021-03-05-20-17-55.png)
### 容器的定义和初始化
![](2021-03-05-20-26-48.png)
### 容器的赋值和交换
![](2021-03-05-20-29-29.png)
### 容器大小的操作
* size():返回容器中元素的数目
* empty():当size为0是返回true
* maxsize():返回容器所能容纳的最大元素数的值。
### 关系运算符
* 容器支持相等和不等的运算。== !=
* 除了无序关联容器,都支持关系运算(> < >= <=)
* 必须是相同各类型的容器,且元素类型相同才能比较。
## 3 顺序容器的操作
### 向顺序容器中添加元素
![](2021-03-05-20-37-12.png)
* 在尾部添加元素push_back()
* 在头部添加元素push_front()
* 在中间添加元素insert()
### 在顺序容器中访问元素
![](2021-03-05-20-40-51.png)
* 也可以使用迭代器访问元素。
* at会进行安全检查抛出异常。[]不会进行检查。
### 在顺序容器中删除元素
![](2021-03-05-20-42-30.png)
> 操作记忆
> * back、front、push_back、push_front、pop_back、pop_front。是一组首尾相关的操作。
> * insert、at、erase。是一组随机的操作。
### foward_list的特殊操作
![](2021-03-05-20-54-47.png)
### 改变容器的大小
![](2021-03-05-20-56-47.png)
## 4 容器的容量问题
### vector的存储
* vector将元素连续存储
* 容器会申请多余的内存空间以应对可能的元素增长。防止每次添加元素后,需要重新分配内存空间。性能会很慢。
### 管理容量的成员函数
![](2021-03-05-21-13-26.png)

View File

@@ -0,0 +1,23 @@
# 顺序容器
> 目录
> * array
> * vector
> * deque
> * list
> * forward_list
> * string//专门用于字符串访问的容器
## 1 array
> 与数组完全一致,只是定义方式不同。
> 数组不能copy赋值但是array可以copy赋值。
## 2 vector
## 3 deque
## 4 foward_list
## 5 list

View File

@@ -0,0 +1,39 @@
# 容器适配器
> 目录
> * stack
> * queue
> * priority_queue
## 0 简介
### 概念
适配器 (adaptor) 是标准库巾的一个通用概念。容器、法代糕和1函数〈歪吕 都有适配器。 本质上, 一个适配器是一种机制, 能使某种事物的行为看起来像另外一种事 物一样。。一个容器适配器接受一种己有的容器类型, 使其行为看起来像一利1不同的类型。
### 容器适配器的操作
![](2021-03-05-21-29-55.png)
## 1 stack
### 概念
### 特有操作
![](2021-03-05-21-40-50.png)
## 2 queue和priority_queue
### 概念
### 特有操作
![](2021-03-05-21-41-56.png)

22
C++/标准库/2.3.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include<iostream>
#include<stack>
#include<vector>
#include<deque>
using namespace std;
int main(){
//stack test
deque<int> deq{2,3,4,5};
stack<int> stk{deq};
int m = stk.top();
stk.pop();
stk.push(111);
cout<<m<<endl;
for(int n:deq){
cout<<n<<endl;
}
return 0;
}

0
C++/标准库/2.cpp Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

View File

@@ -0,0 +1,47 @@
# 迭代器
## 0 基础
### 迭代器范围
> begin和end被容器使用了可以用front和back作为游标。
左闭右开区间
```
[begin,end]
```
### 使用迭代器进行遍历
> 遍历方法有三种下标遍历、范围for遍历、迭代器遍历
```
container c;
first = c.begin();
last = c.end();
while(first != last){
cout<<*first<<endl;//指向元素的游标指针。
++begin;
}
```
### 迭代器的类型
* 正常迭代器
```
begin()
end()
```
* 反向迭代器
```
rbegin()
rend()
```
* 常量迭代器
```
cbegin()
cend()
```
### 操作冲突
容器操作可能会使迭代器实效。

0
C++/标准库/3.cpp Normal file
View File

24
C++/标准库/4 算法.md Normal file
View File

@@ -0,0 +1,24 @@
# 算法
## 1 算法概览
### 头文件
#include<algorithm>//标准库算法
#include<numeric>//用于数值处理相关的函数
#include<functional>//函数相关的东西
### 说明
* 容器的迭代器零算法不依赖于容器。但算法依赖于元素类型的操作。
* 标准库仅仅提供了100多个算法
### 分类
* 只读算法。不改变元素的值
* 更易算法。该表容器的元素
## 2

View File

@@ -0,0 +1,32 @@
## 1 string容器的操作
> 参考顺序容器部分
## 2 string字符串的操作
### string的构造方法
![](2021-03-05-21-16-10.png)
### 子字符串操作
![](2021-03-05-21-16-47.png)
### 修改string的操作
![](2021-03-05-21-20-31.png)
### string搜索操作
![](2021-03-05-21-19-36.png)
### compare函数
![](2021-03-05-21-22-09.png)
### 数值转换
![](2021-03-05-21-23-27.png)

View File

107
C++/标准库/7 IO.md Normal file
View File

@@ -0,0 +1,107 @@
# IO
> IO关系图
![](2021-03-05-16-12-52.png)
![](2021-03-05-16-29-19.png)
> 目录
> * 输入输出流iostream
> * 文件输入输出流fstream
> * 字符串输入输出流sstream
## 0 stream基础知识
### IO对象没有拷贝或赋值
### 管理IO的状态
* 用来记录stream可能出现的状态。
![](2021-03-05-19-15-08.png)
* 使用iostate对象来记录和管理io的状态
* `>> <<`流运算符流函数会返回io的运行状态如果成功则会返回true否则返回false
```
#include<iostream>
using namespace std;
int main(){
//IO state test
int a{3};
cin>>a;
auto old_state = cin.rdstate();//返回s当前的状态
cout<<old_state<<endl;//输出状态
cout<<cin.eof()<<endl;//是否终止符
cout<<cin.fail()<<endl;//是否IO过程错误但未崩溃
cout<<cin.bad()<<endl;//是否崩溃
cout<<cin.good()<<endl;//是否正产
cin.clear();//充值最初的状态
cin.setstate(old_state);//设置流的状态
int word =0;
while(cin>>word){//>>函数返回流的状态。如果成功则返回true
}
return 0;
}
```
### 管理输出缓冲区
导致缓冲区刷新的方法
* 程序正常结束main函数return之后缓冲区刷新
* 缓冲区满时,缓冲区刷新。
* 流操纵符endl、flush、ends刷新缓冲区
* 每个输出操作后使用流操纵符unitbuf设置流的内部状态来清空缓冲区。unitbuf是流的属性。
* 一个输出流可能被关联到另一个流。关联到流的缓冲区会被刷新。cin、cerr、cout相互关联。
```
cout<<""<<endl;
cout<<""<<flush;
cout<<""<<ends;
cout<<unitbuf;//所有的输出操作后立即刷新缓冲区
cout<<nounitbuf;//回到正常的刷新方式
```
## 1 iostream
## 2 fstream
### 文件流的方法
### 文件模式
![](2021-03-05-19-42-28.png)
### 实例
## 3 sstream
### string流
```
//string stream
struct Person{
string name;
string phone;
};
vector<Person> p;
string line;
if(getline(cin,line)){
Person pp;
stringstream record(line);
record>>pp.name;
record>>pp.phone;
p.push_back(pp);
}
cout<<p[0].name<<endl;
cout<<p[0].phone<<endl;
```

45
C++/标准库/7.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int main(){
// //IO state test
// int a{3};
// cin>>a;
// auto old_state = cin.rdstate();//返回s当前的状态
// cout<<old_state<<endl;//输出状态
// cout<<cin.eof()<<endl;//是否终止符
// cout<<cin.fail()<<endl;//是否IO过程错误但未崩溃
// cout<<cin.bad()<<endl;//是否崩溃
// cout<<cin.good()<<endl;//是否正产
// cin.clear();//充值最初的状态
// cin.setstate(old_state);//设置流的状态
// int word =0;
// while(cin>>word){//>>函数返回流的状态。如果成功则返回true
// }
// return 0;
//string stream
struct Person{
string name;
string phone;
};
vector<Person> p;
string line;
if(getline(cin,line)){
Person pp;
stringstream record(line);
record>>pp.name;
record>>pp.phone;
p.push_back(pp);
}
cout<<p[0].name<<endl;
cout<<p[0].phone<<endl;
}

View File

View File

View File

View File

@@ -2,6 +2,7 @@
> 参考文献
> * [谈谈C++中各种初始化方式](https://blog.csdn.net/u014359097/article/details/50788911)
> * [C++的各种初始化方式](https://www.cnblogs.com/pluse/p/7088880.html)
> * [初始化的区别](https://www.zhihu.com/question/36735960)
## 1 初始化方法
C++中的初始化主要包括五种:

View File

@@ -0,0 +1,40 @@
# 三者的区别
> 参考文献
> * [区别](https://blog.csdn.net/weixin_34372728/article/details/93370550?utm_medium=distribute.pc_feed_404.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&dist_request_id=1328602.12458.16149341728211257&depth_1-utm_source=distribute.pc_feed_404.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecas)
## 1 说明
### C中的NULL
* C中使用 ((void*)0)表示空指针。NULL会被替换为 ((void*)0)
```
int *i = NULL;
foo_t *f = NULL;
```
```
#define NULL ((void*)0)
```
### C++中的NULL
* C++中void* 不能进行前置类型转换成其他类型的NULL所以int* 类型的空指针不能使用void* 代替,则原来的宏定义无法使用。
* C++中使用0来表示空指针。
```
#ifdef __cplusplus ---简称cpp c++ 文件
#define NULL 0
#else
#define NULL ((void *)0)
#endif
```
### C++中的0
* 重载过程中会出现问题因为空指针会被识别为整形的0从而导致重载出现错误。
* 0指针的二义性。
![](2021-03-05-17-00-11.png)
### C++中的nullptr
* nullptr 关键字被自动转换为各种pointer类型。但他不会不转换为任何证书类型。

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB