diff --git a/.vscode/settings.json b/.vscode/settings.json index 662fc7c5..e7abb927 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,9 @@ "list": "cpp", "vector": "cpp", "xutility": "cpp", - "regex": "cpp" + "regex": "cpp", + "chrono": "cpp", + "iostream": "cpp", + "ostream": "cpp" } } \ No newline at end of file diff --git a/C++/标准库/1 通用工具.md b/C++/标准库/1 通用工具.md index 07b84528..9c56a495 100644 --- a/C++/标准库/1 通用工具.md +++ b/C++/标准库/1 通用工具.md @@ -47,19 +47,6 @@ make_tuple(22,44,"helo");//元素类型自动推导 ![](2021-03-07-17-33-43.png) ## 2 smart pointer 智能指针 - -> 动态内存分配中有。 - -### 头文件 - -``` -#include -``` - -## 2.1 shared_ptr -## 2.2 weak_ptr -## 2.3 unique_ptr - ## 3 极值 ### 头文件 @@ -117,7 +104,12 @@ int main(){ ## 7.2 Duration时间段 ### duration定义的时间单位 +* duraton由一个数值和ratio(时间比例)共同组成。 +``` +// 时间单位*计数数量=持续时间 +ratio * tick =duration +``` ``` //自定义时间单位 std::chrono::duration> @@ -129,11 +121,38 @@ milliseconds; seconds; minutes; hours; +//这些都是可以使用的duration常量 + +hours aDay(24);//表示一天的duration ``` +### 算数运算 +* 赋值初始化可以使得它在不同的时间单位之间,快速转换。 +![](2021-03-07-21-42-10.png) + +### 其他操作 + +![](2021-03-07-21-42-40.png) +## 7.3 Clock和Timepoint +### 定义 +* Clock定义了一个epoch和一个tick周期。用来表示时间的起点和时间的计数方式。 + +* Timepoint某个特定的时间点。 + +### Clock时钟 + +![](2021-03-07-21-53-28.png) + +### 系统提供了三个clock + +1. system_clock。timepoint关联值现行的系统的及时时钟。包含函数to_time_t(),from_time_t()。可以与C的time_t进行转换 +2. steady_clock。稳定前进 +3. high_resolution_clock 带有最短tick周期的clock。 + + ## 7.4 C、POSIX ### 头文件 @@ -145,26 +164,7 @@ hours; ![](2021-03-07-19-28-19.png) -## 8 bitset类型 - -### 头文件 - -``` -#include -``` - -### 定义和初始化 -``` -bitset<32> bitvec(1U); -``` -* 编号从0开始的二进制位被称为低位。编号31结束的二进制位被称为高位。 -![](2021-03-07-19-46-47.png) - - -### bitset操作 -* bitset支持位运算 -![](2021-03-07-19-50-56.png) - +## 8 ## 9 随机数 @@ -200,4 +200,17 @@ e.seed(323); * 常见的随机分布类型 ``` uniform_real_distribution<> -``` \ No newline at end of file +``` + +## 10 C++的C兼容 + +### cstddef + +![](2021-03-07-22-11-44.png) +### cstdlib + +![](2021-03-07-22-12-19.png) + +### csting + +![](2021-03-07-22-12-27.png) \ No newline at end of file diff --git a/C++/标准库/1.cpp b/C++/标准库/1.cpp index 8cd6c0c0..e0e35765 100644 --- a/C++/标准库/1.cpp +++ b/C++/标准库/1.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace std; int main(){ @@ -9,4 +10,10 @@ int main(){ default_random_engine e; cout< +``` + +### 定义和初始化 +``` +bitset<32> bitvec(1U); +``` +* 编号从0开始的二进制位被称为低位。编号31结束的二进制位被称为高位。 +![](2021-03-07-19-46-47.png) + + +### bitset操作 +* bitset支持位运算 +![](2021-03-07-19-50-56.png) + diff --git a/C++/标准库/2021-03-07-21-42-10.png b/C++/标准库/2021-03-07-21-42-10.png new file mode 100644 index 00000000..1c3c500c Binary files /dev/null and b/C++/标准库/2021-03-07-21-42-10.png differ diff --git a/C++/标准库/2021-03-07-21-42-40.png b/C++/标准库/2021-03-07-21-42-40.png new file mode 100644 index 00000000..a8cee931 Binary files /dev/null and b/C++/标准库/2021-03-07-21-42-40.png differ diff --git a/C++/标准库/2021-03-07-21-53-28.png b/C++/标准库/2021-03-07-21-53-28.png new file mode 100644 index 00000000..f8fc5fa0 Binary files /dev/null and b/C++/标准库/2021-03-07-21-53-28.png differ diff --git a/C++/标准库/2021-03-07-22-11-44.png b/C++/标准库/2021-03-07-22-11-44.png new file mode 100644 index 00000000..4d4b6b2b Binary files /dev/null and b/C++/标准库/2021-03-07-22-11-44.png differ diff --git a/C++/标准库/2021-03-07-22-12-19.png b/C++/标准库/2021-03-07-22-12-19.png new file mode 100644 index 00000000..95a4a60a Binary files /dev/null and b/C++/标准库/2021-03-07-22-12-19.png differ diff --git a/C++/标准库/2021-03-07-22-12-27.png b/C++/标准库/2021-03-07-22-12-27.png new file mode 100644 index 00000000..d6e11243 Binary files /dev/null and b/C++/标准库/2021-03-07-22-12-27.png differ diff --git a/C++/标准库/2021-03-07-22-33-59.png b/C++/标准库/2021-03-07-22-33-59.png new file mode 100644 index 00000000..ebeaabae Binary files /dev/null and b/C++/标准库/2021-03-07-22-33-59.png differ diff --git a/C++/标准库/3 迭代器.md b/C++/标准库/3 迭代器.md index 7e683c97..9833286c 100644 --- a/C++/标准库/3 迭代器.md +++ b/C++/标准库/3 迭代器.md @@ -1,13 +1,16 @@ # 迭代器 -## 0 基础 - +## 1 基础 +### 头文件 +``` +#include +``` ### 迭代器范围 > begin和end被容器使用了,可以用front和back作为游标。 左闭右开区间 ``` -[begin,end] +[begin,end) ``` ### 使用迭代器进行遍历 @@ -47,7 +50,7 @@ cend() 容器操作可能会使迭代器实效。 -## 2 迭代器类型 +## 2 迭代器类型——迭代器的适配器 除了容器定义的迭代器之外,标准库头文件iterator总额外定义了几种迭代器。 * 插入迭代器。绑定到一个容器上,用来向容器中国插入元素 @@ -61,6 +64,7 @@ cend() ![](2021-03-06-14-23-15.png) +![](2021-03-07-22-33-59.png) * back_inserter:创建一个push_back迭代器 * front_inserter:创建一个push_front迭代器 * inserter:创建一个insert迭代器。接受一个指向容器的迭代器。元素将被插入到给定迭代器所表示的元素之前。 diff --git a/C++/标准库/4 算法.md b/C++/标准库/4 算法.md index e9716285..208ad83c 100644 --- a/C++/标准库/4 算法.md +++ b/C++/标准库/4 算法.md @@ -3,46 +3,248 @@ ## 1 泛型算法概览 -### 头文件 -#include//标准库算法 -#include//用于数值处理相关的函数 -#include//函数相关的东西 + ### 说明 * 容器的**迭代器**使得算法不依赖于容器。但算法依赖于元素类型的操作。 * 标准库仅仅提供了100多个算法 +### 头文件 +| 头文件 | 功能 | +|---|---| +| `` | 算法函数 | +| `` | 数值算法 | +| `` | 函数对象/仿函数 | + ### 分类 -* 只读算法。不改变元素的值 -* 更易算法。该表容器的元素 +| No. | 分类 | +|---|---| +| 1 | 非可变序列算法Non-modifying sequence operations(不直接修改容器内容的算法。) | +| 2 | 可变序列算法Modifying sequence operations(可以修改容器内容的算法。) | +| 3 | 排序算法Sorting/Partitions/Binary search/(对序列排序、合并、搜索算法操作。) | +| 4 | 数值算法Merge/Heap/Min/max(对容器内容进行数值计算。) | -### 常用算法示例 -* 只读算法 -accumulate() +## 1.1 基础算法 +### 填充 -* 判断算法 -equal() +| 函数 | 作用 | +|---|---| +| fill(beg,end,val) | 将值val赋给[beg,end)范围内的所有元素。 | +| fill_n(beg,n,val) | 将值val赋给[beg,beg+n)范围内的所有元素。 | +| generate(beg,end,func) | 连续调用函数func填充[beg,end)范围内的所有元素。 | +| generate_n(beg,n,func) | 连续调用函数func填充[beg,beg+n)范围内的所有元素。 | -* 写容器算法 +### 遍历、变换 -fill() +| 函数 | 作用 | +|---|---| +| for_each(beg,end,func) | 将[beg,end)范围内所有元素依次调用函数func,返回func。不修改序列中的元素。 | +| transform(beg,end,res,unary) | 将[beg,end)范围内所有元素依次调用函数unary,结果放入res中。 | +| transform(beg2,end1,beg2,res,binary) | 将[beg,end)范围内所有元素与[beg2,beg2+end-beg)中所有元素依次调用函数unary,结果放入res中。 | -* 拷贝算法 +### 最大最小 +| 函数 | 作用 | +|---|---| +| max(a,b) | 返回两个元素中较大一个。 | +| max(a,b,cmp) | 使用自定义比较操作cmp,返回两个元素中较大一个。 | +| max_element(beg,end) | 返回一个ForwardIterator,指出[beg,end)中最大的元素。 | +| max_element(beg,end,cmp) | 使用自定义比较操作cmp,返回一个ForwardIterator,指出[beg,end)中最大的元素。 | +| min(a,b) | 返回两个元素中较小一个。 | +| min(a,b,cmp) | 使用自定义比较操作cmp,返回两个元素中较小一个。 | +| min_element(beg,end) | 返回一个ForwardIterator,指出[beg,end)中最小的元素。 | +| min_element(beg,end,cmp) | 使用自定义比较操作cmp,返回一个ForwardIterator,指出[beg,end)中最小的元素。 | -copy() +## 1.2 排序算法(12个) -* 重排算法 +### 排序 -sort() -unique() +| 函数 | 作用 | +|---|---| +| sort(beg,end) | 默认升序重新排列元素 | +| sort(beg,end,comp) | 使用函数comp代替比较操作符执行sort()。 | +| partition(beg,end,pred) | 元素重新排序,使用pred函数,把结果为true的元素放在结果为false的元素之前。 | +| stable_sort(beg,end) | 与sort()类似,保留相等元素之间的顺序关系。 | +| stable_sort(beg,end,pred) | 使用函数pred代替比较操作符执行stable_sort()。 | +| stable_partition(beg,end) | 与partition()类似,保留容器中的相对顺序。 | +| stable_partition(beg,end,pred) | 使用函数pred代替比较操作符执行stable_partition()。 | +| partial_sort(beg,mid,end) | 部分排序,被排序元素个数放到[beg,end)内。 | +| partial_sort(beg,mid,end,comp) | 使用函数comp代替比较操作符执行partial_sort()。 | +| partial_sort_copy(beg1,end1,beg2,end2) | 与partial_sort()类似,只是将[beg1,end1)排序的序列复制到[beg2,end2)。 | +| partial_sort_copy(beg1,end1,beg2,end2,comp) | 使用函数comp代替比较操作符执行partial_sort_copy()。 | +| nth_element(beg,nth,end) | 单个元素序列重新排序,使所有小于第n个元素的元素都出现在它前面,而大于它的都出现在后面。 | +| nth_element(beg,nth,end,comp) | 使用函数comp代替比较操作符执行nth_element()。 | -* 查找算法 +### 反转 + +| 函数 | 作用 | +|---|---| +| reverse(beg,end) | 元素重新反序排序。 | +| reverse_copy(beg,end,res) | 与reverse()类似,结果写入res。 | +| rotate(beg,mid,end) | 元素移到容器末尾,由mid成为容器第一个元素。 | +| rotate_copy(beg,mid,end,res) | 与rotate()类似,结果写入res | + +### 随机 + + +| 函数 | 作用 | +|---|---| +| random_shuffle(beg,end) | 元素随机调整次序。 | +| random_shuffle(beg,end,gen) | 使用函数gen代替随机生成函数执行random_shuffle()。 | + +## 1.3 查找算法(13个) +判断容器中是否包含某个值 +### 统计 + +| 函数 | 作用 | +|---|---| +| count(beg,end,val) | 利用==操作符,对[beg,end)的元素与val进行比较,返回相等元素个数。 | +| count_if(beg,end,pred) | 使用函数pred代替==操作符执行count()。 | + +### 查找 + +| 函数 | 作用 | +|---|---| +| find(beg,end,val) | 利用==操作符,对[beg,end)的元素与val进行比较。当匹配时结束搜索,返回该元素的InputIterator。 | +| find_if(beg,end,pred) | 使用函数pred代替==操作符执行find()。 | +| find_first_of(beg1,end1,beg2,end2) | 在[beg1,end1)范围内查找[beg2,end2)中任意一个元素的第一次出现。返回该元素的Iterator。 | +| find_first_of(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行find_first_of()。返回该元素的Iterator。 | +| find_end(beg1,end1,beg2,end2) | 在[beg1,end1)范围内查找[beg2,end2)最后一次出现。找到则返回最后一对的第一个ForwardIterator,否则返回end1。 | +| find_end(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行find_end()。返回该元素的Iterator。 | +| adjacent_find(beg,end) | 对[beg,end)的元素,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator。否则返回end。 | +| adjacent_find(beg,end,pred) | 使用函数pred代替==操作符执行adjacent_find()。 | + +### 搜索 + +| 函数 | 作用 | +|---|---| +| search(beg1,end1,beg2,end2) | 在[beg1,end1)范围内查找[beg2,end2)首一次出现,返回一个ForwardIterator,查找成功,返回[beg1,end1)内第一次出现[beg2,end2)的位置,查找失败指向end1。 | +| search(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行search()。 | +| search_n(beg,end,n,val) | 在[beg,end)范围内查找val出现n次的子序列 | +| search_n(beg,end,n,val,pred) | 使用函数pred代替==操作符执行search_n()。 | +| binary_search(beg,end,val) | 在[beg,end)中查找val,找到返回true。 | +| binary_search(beg,end,val,comp) | 使用函数comp代替比较操作符执行binary_search()。 | + +### 边界 + +| 函数 | 作用 | +|---|---| +| lower_bound(beg,end,val) | 在[beg,end)范围内的可以插入val而不破坏容器顺序的第一个位置,返回一个ForwardIterator。 | +| lower_bound(beg,end,val,comp) | 使用函数comp代替比较操作符执行lower_bound()。 | +| upper_bound(beg,end,val) | 在[beg,end)范围内插入val而不破坏容器顺序的最后一个位置,该位置标志一个大于val的值,返回一个ForwardIterator。 | +| upper_bound(beg,end,val,comp) | 使用函数comp代替比较操作符执行upper_bound()。 | +| equal_range(beg,end,val) | 返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。 | +| equal_range(beg,end,val,comp) | 使用函数comp代替比较操作符执行lower_bound()。 | + +## 1.4 删除和替换算法15个 + +### 复制 + +| 函数 | 作用 | +|---|---| +| copy(beg,end,res) | 复制[beg,end)到res | +| copy_backward(beg,end,res) | 与copy()相同,不过元素是以相反顺序被拷贝。 | + +### 移除 + +| 函数 | 作用 | +|---|---| +| remove(beg,end,val) | 删除[beg,end)内所有等于val的元素。注意,该函数不是真正删除函数。 | +| remove_if(beg,end,pred) | 删除[beg,end)内pred结果为true的元素。 | +| remove_copy(beg,end,res,val) | 将所有不等于val元素复制到res,返回OutputIterator指向被拷贝的末元素的下一个位置。 | +| remove_copy_if(beg,end,res,pred) | 将所有使pred结果为true的元素拷贝到res。 | + +### 替换 + +| 函数 | 作用 | +|---|---| +| replace(beg,end,oval,nval) | 将[beg,end)内所有等于oval的元素都用nval代替。 | +| replace_copy(beg,end,res,oval,nval) | 与replace()类似,不过将结果写入res。 | +| replace_if(beg,end,pred,nval) | 将[beg,end)内所有pred为true的元素用nval代替。 | +| replace_copy_if(beg,end,res,pred,nval) | 与replace_if(),不过将结果写入res。 | + +### 去重 + +| 函数 | 作用 | +|---|---| +| unique(beg,end) | 清除序列中相邻重复元素,不能真正删除元素。重载版本使用自定义比较操作。 | +| unique(beg,end,pred) | 将所有使pred结果为true的相邻重复元素去重。 | +| unique_copy(beg,end,res) | 与unique类似,不过把结果输出到res。 | +| unique_copy(beg,end,res,pred) | 与unique类似,不过把结果输出到res。 | + +### 交换 + +| 函数 | 作用 | +|---|---| +| swap(a,b) | 交换存储在a与b中的值。 | +| swap_range(beg1,end1,beg2) | 将[beg1,end1)内的元素[beg2,beg2+beg1-end1)元素值进行交换。 | +| iter_swap(it_a,it_b) | 交换两个ForwardIterator的值。 | + +## 1.5 算数算法(4个) + +| 函数 | 作用 | +|---|---| +| accumulate(beg,end,val) | 对[beg,end)内元素之和,加到初始值val上。 | +| accumulate(beg,end,val,binary) | 将函数binary代替加法运算,执行accumulate()。 | +| partial_sum(beg,end,res) | 将[beg,end)内该位置前所有元素之和放进res中。 | +| partial_sum(beg,end,res,binary) | 将函数binary代替加法运算,执行partial_sum()。 | +| adjacent_difference(beg1,end1,res) | 将[beg,end)内每个新值代表当前元素与上一个元素的差放进res中。 | +| adjacent_difference(beg1,end1,res,binary) | 将函数binary代替减法运算,执行adjacent_difference()。 | +| inner_product(beg1,end1,beg2,val) | 对两个序列做内积(对应元素相乘,再求和)并将内积加到初始值val上。 | +| inner_product(beg1,end1,beg2,val,binary1,binary2) | 将函数binary1代替加法运算,将binary2代替乘法运算,执行inner_product()。 | + +## 1.6 关系算法(4个) + +| 函数 | 作用 | +|---|---| +| equal(beg1,end1,beg2) | 判断[beg1,end1)与[beg2,end2)内元素都相等 | +| equal(beg1,end1,beg2,pred) | 使用pred函数代替默认的==操作符。 | +| includes(beg1,end1,beg2,end2) | 判断[beg1,end1)是否包含[beg2,end2),使用底层元素的<操作符,成功返回true。重载版本使用用户输入的函数。 | +| includes(beg1,end1,beg2,end2,comp) | 将函数comp代替<操作符,执行includes()。 | +| lexicographical_compare(beg1,end1,beg2,end2) | 按字典序判断[beg1,end1)是否小于[beg2,end2) | +| lexicographical_compare(beg1,end1,beg2,end2,comp) | 将函数comp代替<操作符,执行lexicographical_compare()。 | +| mismatch(beg1,end1,beg2) | 并行比较[beg1,end1)与[beg2,end2),指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的end。 | +| mismatch(beg1,end1,beg2,pred) | 使用pred函数代替默认的==操作符。 | + +## 1.7 集合算法(6个) + +| 函数 | 作用 | +|---|---| +| merge(beg1,end1,beg2,end2,res) | 合并[beg1,end1)与[beg2,end2)存放到res。 | +| merge(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行merge()。 | +| inplace_merge(beg,mid,end) | 合并[beg,mid)与[mid,end),结果覆盖[beg,end)。 | +| inplace_merge(beg,mid,end,cmp) | 将函数comp代替<操作符,执行inplace_merge()。 | +| set_union(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素并集存放到res。 | +| set_union(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_union()。 | +| set_intersection(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素交集存放到res。 | +| set_intersection(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_intersection()。 | +| set_difference(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素内差集存放到res。 | +| set_difference(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_difference()。 | +| set_symmetric_difference(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素外差集存放到res。 | + +## 1.8 排序组合算法(两个) +| 函数 | 作用 | +|---|---| +| next_permutation(beg,end) | 取出[beg,end)内的下移一个排列。 | +| next_permutation(beg,end,comp) | 将函数comp代替<操作符,执行next_permutation()。 | +| prev_permutation(beg,end) | 取出[beg,end)内的上移一个排列。 | +| prev_permutation(beg,end,comp) | 将函数comp代替<操作符,执行prev_permutation()。 | + +## 1.9 堆算法(4个) + +| 函数 | 作用 | +|---|---| +| make_heap(beg,end) | 把[beg,end)内的元素生成一个堆。 | +| make_heap(beg,end,comp) | 将函数comp代替<操作符,执行make_heap()。 | +| pop_heap(beg,end) | 重新排序堆。它把first和last-1交换,然后重新生成一个堆。可使用容器的back来访问被"弹出"的元素或者使用pop_back进行真正的删除。并不真正把最大元素从堆中弹出。 | +| pop_heap(beg,end,comp) | 将函数comp代替<操作符,执行pop_heap()。 | +| push_heap(beg,end) | 假设first到last-1是一个有效堆,要被加入到堆的元素存放在位置last-1,重新生成堆。在指向该函数前,必须先把元素插入容器后。 | +| push_heap(beg,end,comp) | 将函数comp代替<操作符,执行push_heap()。 | +| sort_heap(beg,end) | 对[beg,end)内的序列重新排序。 | +| sort_heap(beg,end,comp) | 将函数comp代替<操作符,执行push_heap()。 | -find() -find_if() ## 2 泛型算法的结构