通用工具

This commit is contained in:
yinkanglong_lab
2021-03-07 21:32:33 +08:00
parent c0c6441ede
commit f7c8e737eb
36 changed files with 520 additions and 7 deletions

View File

@@ -8,6 +8,7 @@
"initializer_list": "cpp",
"list": "cpp",
"vector": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"regex": "cpp"
}
}

View File

@@ -69,7 +69,7 @@ auto add(T1 x,T2 y)->decltype(x+y);
## 2 一般概念
### 命名空间
### 2.1 命名空间
使用方法
@@ -92,9 +92,26 @@ using namespace std;
cout<<hex<<3.4<<endl;
```
### 头文件
### 错误和异常处理
### 2.2 头文件
```
#include<iostream>
#include<string>
```
### 2.3 错误和异常处理
* 标准错误和异常如下:
![](2021-03-05-17-30-07.png)
![](2021-03-05-17-30-07.png)
### 2.4 可被调用的对象
callable object。后边可以使用调用运算符()。
* 一个函数。接受额外传入的args作为实参argument
* 一个指向成员函数的指针。
* 一个函数对象。function object重载了调用运算符operator()
* 一个lambda表达式
### 2.5 并发与多线程
### 2.6 分配器Allocator

View File

@@ -1,3 +1,203 @@
# 通用工具
> 目录
> * pair和tuple
> * 智能指针
> * 数值极值
> * type trait 和type utility
> * 辅助函数
> * clock和timer
> * bitset
> * 随机数
## 1 pair和Tuple
## 1.1 pair
### 头文件
```
#include<utility>
```
### pair定义
![](2021-03-07-17-22-16.png)
### pair操作
![](2021-03-07-17-22-38.png)
## 1.2 tuple
### 头文件
```
#include<tuple>
```
### 定义
* 扩展pair的概念拥有任意数量的元素。是一个异质的元素序列。
![](2021-03-07-17-29-36.png)
### 操作
![](2021-03-07-17-32-39.png)
```
tuple<int,float,string> t1{2,3.4,"yin"};
get<1>(t1);//获取t1的第一个元素
make_tuple(22,44,"helo");//元素类型自动推导
```
![](2021-03-07-17-33-43.png)
## 2 smart pointer 智能指针
> 动态内存分配中有。
### 头文件
```
#include<memory>
```
## 2.1 shared_ptr
## 2.2 weak_ptr
## 2.3 unique_ptr
## 3 极值
### 头文件
```
#include<limits>
```
### 使用
```
#include<limits>
using namespace std;
int main(){
cout<<numeric_limits<short>::max()<<endl;//输出short类型最大值
}
```
## 4 type trait和type utility
## 5 辅助函数
### 最大最小值
```
#include<algorithm>
```
![](2021-03-07-17-47-32.png)
### 交换
```
#include<utility>
```
![](2021-03-07-17-48-43.png)
## 6 class ratio编译期分数
## 7 Clock和Timer
## 7.1 Chrono概览
> 相关术语duration,timepoint,tick,epoch,lock,timer,date,time。chrono目标处理timer和clock在不同系统中更可能不同的问题。强化实践的精准度。
### 头文件
```
#include<chrono>//额外的是艰苦
```
### 术语
* duration时间段某个时间单位上tick数。
* timepoint时间点duration和epoch起始点的组合。
* clock时钟每个时钟都有自己的epoch起始点——
## 7.2 Duration时间段
### duration定义的时间单位
```
//自定义时间单位
std::chrono::duration<double,std::ratio<60>>
//定义好的时间单位
nanoseconds;
microseconds;
milliseconds;
seconds;
minutes;
hours;
```
## 7.4 C、POSIX
### 头文件
```
#include<ctime>
```
### 常用操作
![](2021-03-07-19-28-19.png)
## 8 bitset类型
### 头文件
```
#include<bitset>
```
### 定义和初始化
```
bitset<32> bitvec(1U);
```
* 编号从0开始的二进制位被称为低位。编号31结束的二进制位被称为高位。
![](2021-03-07-19-46-47.png)
### bitset操作
* bitset支持位运算
![](2021-03-07-19-50-56.png)
## 9 随机数
### 头文件
```
#include<random>
```
### 随机数库的组成
![](2021-03-07-21-12-23.png)
### 随机数引擎的操作
![](2021-03-07-21-14-33.png)
* 编译器会自动选择一个随机数引擎作为default_random_engine的类型的引擎。
```
default_random_engine e;
cout<<e()<<endl;
```
### 随机数分布的操作
* 需要给默认的随机数引擎选择一个分布序列。
```
uniform_int_distribution<unsigned> u<0,9>;
default_random_engine e(323);//指定初始化的种子
e.seed(323);
```
![](2021-03-07-21-20-51.png)
* 常见的随机分布类型
```
uniform_real_distribution<>
```

View File

@@ -0,0 +1,12 @@
#include<iostream>
#include<utility>
#include<tuple>
#include<random>
using namespace std;
int main(){
tuple<int,double,string> t{1,4,"fei"};
default_random_engine e;
cout<<e()<<endl;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

View File

@@ -0,0 +1,222 @@
# 正则表达式
## 1 正则表达式组件
## 1.1 正则表达式的基础用法
### 头文件
```
#include<regex>
```
### 库组件
![](2021-03-07-19-53-20.png)
### 正则表达式对象
![](2021-03-07-20-52-19.png)
### 操作
![](2021-03-07-19-55-20.png)
![](2021-03-07-20-34-45.png)
```C++
#include<iostream>
#include<regex>
using namespace std;
int main(){
//i 必须在e之前除非在c之后
string pattern("[^c]ei");
pattern ="[a-zA-Z]"+pattern+"[a-zA-Z]";
regex r(pattern);
smatch results;
string test_str="receipt freind theif receive";
if(regex_search(test_str,results,r))
cout<<results.str()<<endl;
else
cout<<results.str()<<endl;
return 0;
}
```
## 1.2 正则表达式的迭代器string对象
### regex迭代器选择
![](2021-03-07-20-36-59.png)
### regex迭代器使用针对string
![](2021-03-07-20-53-15.png)
```
#include<iostream>
#include<regex>
using namespace std;
int main(){
//i 必须在e之前除非在c之后
string pattern("[^c]ei");
pattern ="[a-zA-Z]"+pattern+"[a-zA-Z]";
regex r(pattern);
smatch results;
string test_str="receipt freind theif receive";
if(regex_search(test_str,results,r))
cout<<results.str()<<endl;
else
cout<<results.str()<<endl;
regex r2(pattern, regex::icase);
sregex_iterator end_it;//string
sregex_iterator it(test_str.begin(),test_str.end(),r2);
for(;it != end_it;++it){
cout<<it->str()<<endl;
}
return 0;
}
```
### smatch的操作针对string
![](2021-03-07-21-07-30.png)
## 1.3 子表达式的正则匹配
![](2021-03-07-21-10-18.png)
## 1.4 regex_replace
![](2021-03-07-21-10-01.png)
## 2 正则表达式规则
主要包括四类
* 字符类
* 数量限定符
* 位置限定符
* 特殊符号
### 2.1 字符类
![](2021-03-07-20-17-59.png)
### 2.2 数量限定符
![](2021-03-07-20-18-20.png)
### 2.3 位置限定符
![](2021-03-07-20-18-37.png)
### 2.4 特殊符号
![](2021-03-07-20-18-51.png)
### 2.5 普通字符集及其替换
![](2021-03-07-20-20-11.png)
### 2.6 贪婪模式与非贪婪模式
1. 贪婪模式:正则表达式匹配时,会尽量多的匹配符合条件的内容。
2. 非贪婪模式正则表达式匹配时会尽量少的匹配符合条件的内容也就是说一旦发现匹配符合要求立马就匹配成功而不会继续匹配下去除非有g开启下一组匹配
### 2.7 特殊规则
* '[:alnum:]' 匹配任何字母和数字
Alphanumeric characters: '[:alpha:]' and '[:digit:]'.
* '[:alpha:]' 匹配任何字母
Alphabetic characters: '[:lower:]' and '[:upper:]'.
* '[:blank:]'
Blank characters: space and tab.
* '[:cntrl:]'
Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 ('DEL'). In other character sets, these are the equivalent characters, if any.
* '[:digit:]' 匹配任何数字
Digits: '0 1 2 3 4 5 6 7 8 9'.
* '[:graph:]'
Graphical characters: '[:alnum:]' and '[:punct:]'.
* '[:lower:]' 匹配任何小写字母
Lower-case letters: 'a b c d e f g h i j k l m n o p q r s t u v w
x y z'.
* '[:print:]'
Printable characters: '[:alnum:]', '[:punct:]', and space.
* '[:punct:]' 匹配任何标点符号
Punctuation characters: '! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ' { | } ~'.
* '[:space:]' 匹配空格符
Space characters: tab, newline, vertical tab, form feed, carriage
return, and space.
* '[:upper:]' 匹配任何大写字母
Upper-case letters: 'A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z'.
* '[:xdigit:]' 匹配任何16进制数字
Hexadecimal digits: '0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f'.
### 2.8 正则规则速查表
| **字符** | **描述** |
|--------------|----------------|
| \ | 将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如“n”匹配字符“n”。“\n”匹配一个换行符。串行“\\”匹配“\”而“\(”则匹配“(”。 |
| ^ | 匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性^也匹配“\n”或“\r”之后的位置。 |
| $ | 匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性$也匹配“\n”或“\r”之前的位置。 |
| * | 匹配前面的子表达式零次或多次。例如zo*能匹配“z”以及“zoo”。*等价于{0,}。 |
| + | 匹配前面的子表达式一次或多次。例如“zo+”能匹配“zo”以及“zoo”但不能匹配“z”。+等价于{1,}。 |
| ? | 匹配前面的子表达式零次或一次。例如“do(es)?”可以匹配“does”或“does”中的“do”。?等价于{0,1}。 |
| {n} | n是一个非负整数。匹配确定的n次。例如“o{2}”不能匹配“Bob”中的“o”但是能匹配“food”中的两个o。 |
| {n,} | n是一个非负整数。至少匹配n次。例如“o{2,}”不能匹配“Bob”中的“o”但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”。 |
| {n,m} | m和n均为非负整数其中n<=m。最少匹配n次且最多匹配m次。例如“o{1,3}”将匹配“fooooood”中的前三个o。“o{0,1}”等价于“o?”。请注意在逗号和两个数之间不能有空格。 |
| ? | 当该字符紧跟在任何一个其他限制符(*,+,?{n}{n,}{n,m}后面时匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如对于字符串“oooo”“o+?”将匹配单个“o”而“o+”将匹配所有“o”。 |
| . | 匹配除“\n”之外的任何单个字符。要匹配包括“\n”在内的任何字符请使用像“(.|\n)”的模式。 |
| (pattern) | 匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到在VBScript中使用SubMatches集合在JScript中则使用$0…$9属性。要匹配圆括号字符请使用“\(”或“\)”。 |
| (?:pattern) | 匹配pattern但不获取匹配结果也就是说这是一个非获取匹配不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分是很有用。例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式。 |
| (?=pattern) | 正向肯定预查在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配也就是说该匹配不需要获取供以后使用。例如“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”但不能匹配“Windows3.1”中的“Windows”。预查不消耗字符也就是说在一个匹配发生后在最后一次匹配之后立即开始下一次匹配的搜索而不是从包含预查的字符之后开始。 |
| (?!pattern) | 正向否定预查在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配也就是说该匹配不需要获取供以后使用。例如“Windows(?!95|98|NT|2000)”能匹配“Windows3.1”中的“Windows”但不能匹配“Windows2000”中的“Windows”。预查不消耗字符也就是说在一个匹配发生后在最后一次匹配之后立即开始下一次匹配的搜索而不是从包含预查的字符之后开始 |
| (?<=pattern) | 反向肯定预查,与正向肯定预查类拟,只是方向相反。例如,“(?<=95|98|NT|2000)Windows”能匹配“2000Windows”中的“Windows”但不能匹配“3.1Windows”中的“Windows”。 |
| (?<!pattern) | 反向否定预查,与正向否定预查类拟,只是方向相反。例如“(?<!95|98|NT|2000)Windows”能匹配“3.1Windows”中的“Windows”但不能匹配“2000Windows”中的“Windows”。 |
| x|y | 匹配x或y。例如“z|food”能匹配“z”或“food”。“(z|f)ood”则匹配“zood”或“food”。 |
| [xyz] | 字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”。 |
| [^xyz] | 负值字符集合。匹配未包含的任意字符。例如,“[^abc]”可以匹配“plain”中的“p”。 |
| [a-z] | 字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。 |
| [^a-z] | 负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。 |
| \b | 匹配一个单词边界也就是指单词和空格间的位置。例如“er\b”可以匹配“never”中的“er”但不能匹配“verb”中的“er”。 |
| \B | 匹配非单词边界。“er\B”能匹配“verb”中的“er”但不能匹配“never”中的“er”。 |
| \cx | 匹配由x指明的控制字符。例如\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则将c视为一个原义的“c”字符。 |
| \d | 匹配一个数字字符。等价于[0-9]。 |
| \D | 匹配一个非数字字符。等价于[^0-9]。 |
| \f | 匹配一个换页符。等价于\x0c和\cL。 |
| \n | 匹配一个换行符。等价于\x0a和\cJ。 |
| \r | 匹配一个回车符。等价于\x0d和\cM。 |
| \s | 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r \v]。 |
| \S | 匹配任何非空白字符。等价于[^ \f\n\r\t\v]。 |
| \t | 匹配一个制表符。等价于\x09和\cI。 |
| \v | 匹配一个垂直制表符。等价于\x0b和\cK。 |
| \w | 匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]”。 |
| \W | 匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。 |
| \xn | 匹配n其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如“\x41”匹配“A”。“\x041”则等价于“\x04&1”。正则表达式中可以使用ASCII编码。. |
| \num | 匹配num其中num是一个正整数。对所获取的匹配的引用。例如“(.)\1”匹配两个连续的相同字符。 |
| \n | 标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式则n为向后引用。否则如果n为八进制数字0-7则n为一个八进制转义值。 |
| \nm | 标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式则nm为向后引用。如果\nm之前至少有n个获取则n为一个后跟文字m的向后引用。如果前面的条件都不满足若n和m均为八进制数字0-7则\nm将匹配八进制转义值nm。 |
| \nml | 如果n为八进制数字0-3且m和l均为八进制数字0-7则匹配八进制转义值nml。 |
| \un | 匹配n其中n是一个用四个十六进制数字表示的Unicode字符。例如\u00A9匹配版权符号©。 |

View File

@@ -0,0 +1,27 @@
#include<iostream>
#include<regex>
using namespace std;
int main(){
//i 必须在e之前除非在c之后
string pattern("[^c]ei");
pattern ="[a-zA-Z]"+pattern+"[a-zA-Z]";
regex r(pattern);
smatch results;
string test_str="receipt freind theif receive";
if(regex_search(test_str,results,r))
cout<<results.str()<<endl;
else
cout<<results.str()<<endl;
regex r2(pattern, regex::icase);
sregex_iterator end_it;//string
sregex_iterator it(test_str.begin(),test_str.end(),r2);
for(;it != end_it;++it){
cout<<it->str()<<endl;
}
return 0;
}

View File

@@ -104,4 +104,38 @@ cout<<nounitbuf;//回到正常的刷新方式
}
cout<<p[0].name<<endl;
cout<<p[0].phone<<endl;
```
```
## 4 格式化输入输出
> 使用流操纵符完成格式化输入输出
| **流操纵算子** | **作  用** |
|---|---|
| dec | 以十进制形式输出整数 |
| hex | 以十六进制形式输出整数 |
| oct | 以八进制形式输出整数 |
| fixed | 以普通小数形式输出浮点数 |
| scientific | 以科学计数法形式输出浮点数 |
| left | 左对齐,即在宽度不足时将填充字符添加到右边 |
| right | 右对齐,即在宽度不足时将填充字符添加到左边 |
| setbase(b) | 设置输出整数时的进制b=8、10 或 16 |
| setw(w) | 指定输出宽度为 w 个字符,或输人字符串时读入 w 个字符 |
| setfill(c) | 在指定输出宽度的情况下,输出的宽度不足时用字符 c 填充(默认情况是用空格填充) |
| setprecision(n) | 设置输出浮点数的精度为 n。在使用非 fixed 且非 scientific 方式输出的情况下n 即为有效数字最多的位数,如果有效数字位数超过 n则小数部分四舍五人或自动变为科学计 数法输出并保留一共 n 位有效数字。在使用 fixed 方式和 scientific 方式输出的情况下n 是小数点后面应保留的位数。 |
| setiosflags(flag) | 将某个输出格式标志置为 1 |
| resetiosflags(flag) | 将某个输出格式标志置为 0 |
| boolapha | 把 true 和 false 输出为字符串 |
| noboolalpha | 把 true 和 false 输出为 0、1 |
| showbase | 输出表示数值的进制的前缀 |
| noshowbase | 不输出表示数值的进制.的前缀 |
| showpoint | 总是输出小数点 |
| noshowpoint | 只有当小数部分存在时才显示小数点 |
| showpos | 在非负数值中显示 + |
| noshowpos | 在非负数值中不显示 + |
| skipws | 输入时跳过空白字符 |
| noskipws | 输入时不跳过空白字符 |
| uppercase | 十六进制数中使用 A~E。若输出前缀则前缀输出 0X科学计数法中输出 E |
| *nouppercase | 十六进制数中使用 a~e。若输出前缀则前缀输出 0x科学计数法中输出 e。 |
| internal | 数值的符号(正负号)在指定宽度内左对齐,数值右对 齐,中间由填充字符填充。 |

View File

@@ -12,7 +12,7 @@
- C++primer
- 基础语法√
- 标准库 STL√
- 面向对象
- 面向对象
- 设计模式有道云笔记gitee 设计模式库,书)
- effective 系列
- 系列视频