diff --git a/C++/标准库/0 简介.md b/C++/标准库/0 简介.md new file mode 100644 index 00000000..0ed42153 --- /dev/null +++ b/C++/标准库/0 简介.md @@ -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"<int{cout< +decltype(x+y) add(T1 x,T2 y); + +template +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< +using namespace std; + +int main(){ + int a=1,b=2; + auto l = [](int m,int n)->int{cout< 新标准库的容器壁使用原始的数组实现的数据结构要快很多。经过了精心的优化。 + + +### 确定使用哪种容器 + +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) + diff --git a/C++/标准库/2.1 顺序容器.md b/C++/标准库/2.1 顺序容器.md new file mode 100644 index 00000000..433ea66a --- /dev/null +++ b/C++/标准库/2.1 顺序容器.md @@ -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 diff --git a/C++/标准库/4.关联容器.md b/C++/标准库/2.2 关联容器.md similarity index 100% rename from C++/标准库/4.关联容器.md rename to C++/标准库/2.2 关联容器.md diff --git a/C++/标准库/2.3 容器适配器.md b/C++/标准库/2.3 容器适配器.md new file mode 100644 index 00000000..b9e6eac1 --- /dev/null +++ b/C++/标准库/2.3 容器适配器.md @@ -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) + + + diff --git a/C++/标准库/2.3.cpp b/C++/标准库/2.3.cpp new file mode 100644 index 00000000..23025748 --- /dev/null +++ b/C++/标准库/2.3.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +using namespace std; + +int main(){ + + //stack test + deque deq{2,3,4,5}; + stack stk{deq}; + int m = stk.top(); + stk.pop(); + stk.push(111); + cout< begin和end被容器使用了,可以用front和back作为游标。 +左闭右开区间 +``` +[begin,end] +``` + +### 使用迭代器进行遍历 + +> 遍历方法有三种:下标遍历、范围for遍历、迭代器遍历 +``` +container c; +first = c.begin(); +last = c.end(); +while(first != last){ + cout<<*first<//标准库算法 +#include//用于数值处理相关的函数 +#include//函数相关的东西 + +### 说明 + +* 容器的迭代器零算法不依赖于容器。但算法依赖于元素类型的操作。 +* 标准库仅仅提供了100多个算法 + +### 分类 + +* 只读算法。不改变元素的值 +* 更易算法。该表容器的元素 + + + +## 2 + diff --git a/C++/标准库/5 字符串.md b/C++/标准库/5 字符串.md new file mode 100644 index 00000000..1a79e1c2 --- /dev/null +++ b/C++/标准库/5 字符串.md @@ -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) diff --git a/C++/标准库/6 正则表达式.md b/C++/标准库/6 正则表达式.md new file mode 100644 index 00000000..e69de29b diff --git a/C++/标准库/7 IO.md b/C++/标准库/7 IO.md new file mode 100644 index 00000000..cfa14d9c --- /dev/null +++ b/C++/标准库/7 IO.md @@ -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 +using namespace std; + +int main(){ + + //IO state test + int a{3}; + cin>>a; + auto old_state = cin.rdstate();//返回s当前的状态 + cout<>word){//>>函数返回流的状态。如果成功则返回true + + } + return 0; +} +``` + +### 管理输出缓冲区 + +导致缓冲区刷新的方法 + +* 程序正常结束,main函数return之后,缓冲区刷新 +* 缓冲区满时,缓冲区刷新。 +* 流操纵符endl、flush、ends,刷新缓冲区 +* 每个输出操作后,使用流操纵符unitbuf设置流的内部状态,来清空缓冲区。unitbuf是流的属性。 +* 一个输出流可能被关联到另一个流。关联到流的缓冲区会被刷新。cin、cerr、cout相互关联。 + +``` +cout<<""< p; + string line; + if(getline(cin,line)){ + Person pp; + stringstream record(line); + record>>pp.name; + record>>pp.phone; + p.push_back(pp); + } + cout< +#include +#include +#include +#include +using namespace std; + +int main(){ + + // //IO state test + // int a{3}; + // cin>>a; + // auto old_state = cin.rdstate();//返回s当前的状态 + // cout<>word){//>>函数返回流的状态。如果成功则返回true + + // } + // return 0; + + + //string stream + struct Person{ + string name; + string phone; + }; + vector p; + string line; + if(getline(cin,line)){ + Person pp; + stringstream record(line); + record>>pp.name; + record>>pp.phone; + p.push_back(pp); + } + cout< 参考文献 > * [谈谈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++中的初始化主要包括五种: diff --git a/C++/面试/12.nullptr、NULL、0.md b/C++/面试/12.nullptr、NULL、0.md new file mode 100644 index 00000000..0fe4a154 --- /dev/null +++ b/C++/面试/12.nullptr、NULL、0.md @@ -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类型。但他不会不转换为任何证书类型。 diff --git a/C++/面试/2021-03-05-17-00-11.png b/C++/面试/2021-03-05-17-00-11.png new file mode 100644 index 00000000..3da48bcd Binary files /dev/null and b/C++/面试/2021-03-05-17-00-11.png differ