diff --git a/.vscode/settings.json b/.vscode/settings.json index e7abb927..c88bb975 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "regex": "cpp", "chrono": "cpp", "iostream": "cpp", - "ostream": "cpp" + "ostream": "cpp", + "iomanip": "cpp" } } \ No newline at end of file diff --git a/C++/标准库/1 通用工具.md b/C++/标准库/1 通用工具.md index 9c56a495..af9bf6f6 100644 --- a/C++/标准库/1 通用工具.md +++ b/C++/标准库/1 通用工具.md @@ -159,50 +159,95 @@ hours aDay(24);//表示一天的duration ``` #include ``` +### 概述 +* 有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。 + +* 结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下: +``` +struct tm { + int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61 + int tm_min; // 分,范围从 0 到 59 + int tm_hour; // 小时,范围从 0 到 23 + int tm_mday; // 一月中的第几天,范围从 1 到 31 + int tm_mon; // 月,范围从 0 到 11 + int tm_year; // 自 1900 年起的年数 + int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起 + int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起 + int tm_isdst; // 夏令时 +}; +``` ### 常用操作 ![](2021-03-07-19-28-19.png) -## 8 +### 常用函数 +| 生成时间 | 函数 & 描述 | +|---|---| +| `clock_t clock(void)`| ;该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1。 | +| `time_t time(time_t *time)` | ;该函数返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1。 | -## 9 随机数 +| 格式转换 | 函数 & 描述 | +|---|---| +| `char *ctime(const time_t *time)` | ;该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。 | +| `struct tm *localtime(const time_t *time)**` | ;该函数返回一个指向表示本地时间的 tm 结构的指针。 | +| `char * asctime ( const struct tm * time )` | ;该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。 | +| `struct tm *gmtime(const time_t *time)` | ;该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。 | +| `time_t mktime(struct tm *time)` | ;该函数返回日历时间,相当于 time 所指向结构中存储的时间。 | +| `double difftime ( time_t time2, time_t time1 )` | ;该函数返回 time1 和 time2 之间相差的秒数。 | +| `size_t strftime()` | ;该函数可用于格式化日期和时间为指定的格式。 | -### 头文件 +### 获取当前时间 +```C++ +#include +#include + +using namespace std; + +int main( ) +{ + // 基于当前系统的当前日期/时间 + time_t now = time(0); + + // 把 now 转换为字符串形式 + char* dt = ctime(&now); + + cout << "本地日期和时间:" << dt << endl; + + // 把 now 转换为 tm 结构 + tm *gmtm = gmtime(&now); + dt = asctime(gmtm); + cout << "UTC 日期和时间:"<< dt << endl; +} ``` -#include +### 使用tm格式化当前时间 + +```C++ +#include +#include + +using namespace std; + +int main( ) +{ + // 基于当前系统的当前日期/时间 + time_t now = time(0); + + cout << "1970 到目前经过秒数:" << now << endl; + + tm *ltm = localtime(&now); + + // 输出 tm 结构的各个组成部分 + cout << "年: "<< 1900 + ltm->tm_year << endl; + cout << "月: "<< 1 + ltm->tm_mon<< endl; + cout << "日: "<< ltm->tm_mday << endl; + cout << "时间: "<< ltm->tm_hour << ":"; + cout << ltm->tm_min << ":"; + cout << ltm->tm_sec << endl; +} ``` -### 随机数库的组成 -![](2021-03-07-21-12-23.png) - - -### 随机数引擎的操作 -![](2021-03-07-21-14-33.png) - -* 编译器会自动选择一个随机数引擎作为default_random_engine的类型的引擎。 -``` - default_random_engine e; - cout< u<0,9>; -default_random_engine e(323);//指定初始化的种子 -e.seed(323); -``` - -![](2021-03-07-21-20-51.png) - -* 常见的随机分布类型 -``` -uniform_real_distribution<> -``` - -## 10 C++的C兼容 +## 8 C++的C兼容 ### cstddef diff --git a/C++/标准库/2021-03-07-21-12-23.png b/C++/标准库/2021-03-07-21-12-23.png deleted file mode 100644 index f2d27af9..00000000 Binary files a/C++/标准库/2021-03-07-21-12-23.png and /dev/null differ diff --git a/C++/标准库/2021-03-08-14-03-18.png b/C++/标准库/2021-03-08-14-03-18.png new file mode 100644 index 00000000..3ca689da Binary files /dev/null and b/C++/标准库/2021-03-08-14-03-18.png differ diff --git a/C++/标准库/2021-03-08-14-04-34.png b/C++/标准库/2021-03-08-14-04-34.png new file mode 100644 index 00000000..a40a09b3 Binary files /dev/null and b/C++/标准库/2021-03-08-14-04-34.png differ diff --git a/C++/标准库/2021-03-08-14-05-20.png b/C++/标准库/2021-03-08-14-05-20.png new file mode 100644 index 00000000..0598c6cb Binary files /dev/null and b/C++/标准库/2021-03-08-14-05-20.png differ diff --git a/C++/标准库/2021-03-08-14-06-13.png b/C++/标准库/2021-03-08-14-06-13.png new file mode 100644 index 00000000..6bd0b56b Binary files /dev/null and b/C++/标准库/2021-03-08-14-06-13.png differ diff --git a/C++/标准库/2021-03-08-14-44-22.png b/C++/标准库/2021-03-08-14-44-22.png new file mode 100644 index 00000000..dee6772e Binary files /dev/null and b/C++/标准库/2021-03-08-14-44-22.png differ diff --git a/C++/标准库/2021-03-08-14-44-45.png b/C++/标准库/2021-03-08-14-44-45.png new file mode 100644 index 00000000..b87a3bf7 Binary files /dev/null and b/C++/标准库/2021-03-08-14-44-45.png differ diff --git a/C++/标准库/2021-03-08-14-45-15.png b/C++/标准库/2021-03-08-14-45-15.png new file mode 100644 index 00000000..344626ce Binary files /dev/null and b/C++/标准库/2021-03-08-14-45-15.png differ diff --git a/C++/标准库/2021-03-08-14-45-22.png b/C++/标准库/2021-03-08-14-45-22.png new file mode 100644 index 00000000..9343c1fd Binary files /dev/null and b/C++/标准库/2021-03-08-14-45-22.png differ diff --git a/C++/标准库/2021-03-08-14-46-04.png b/C++/标准库/2021-03-08-14-46-04.png new file mode 100644 index 00000000..82077017 Binary files /dev/null and b/C++/标准库/2021-03-08-14-46-04.png differ diff --git a/C++/标准库/2021-03-08-14-47-09.png b/C++/标准库/2021-03-08-14-47-09.png new file mode 100644 index 00000000..4abc084c Binary files /dev/null and b/C++/标准库/2021-03-08-14-47-09.png differ diff --git a/C++/标准库/4 算法.md b/C++/标准库/4 算法.md index 208ad83c..21d0a36d 100644 --- a/C++/标准库/4 算法.md +++ b/C++/标准库/4 算法.md @@ -1,5 +1,8 @@ # 算法 +> 参考文献 +> * [C++进阶:STL算法总结](https://www.jianshu.com/p/eb554b0943ab) +> * [C++标准库STL算法](https://www.cnblogs.com/larry-xia/p/9497340.html) ## 1 泛型算法概览 diff --git a/C++/标准库/5 字符串.md b/C++/标准库/5 字符串.md index 1a79e1c2..96c23d79 100644 --- a/C++/标准库/5 字符串.md +++ b/C++/标准库/5 字符串.md @@ -30,3 +30,28 @@ ### 数值转换 ![](2021-03-05-21-23-27.png) + + + +### C 字符串转 string + +``` +str ="helloworld"; +string s(str); +``` + +### string 转 C 风格字符串 + +``` +string s("helloworld"); +const char * str = s.c_str(); +``` + +## 3 string相关的外部算法 + +> string因为支持迭代器,所以支持所有的容器模板算法。 + + +## 4 正则匹配 + +> 在正则表达式部分,有专门针对string的正则匹配搜索算法。 diff --git a/C++/标准库/7 IO.md b/C++/标准库/7 IO.md index 03194df2..b4ab2ea4 100644 --- a/C++/标准库/7 IO.md +++ b/C++/标准库/7 IO.md @@ -1,9 +1,9 @@ # IO > IO关系图 - ![](2021-03-05-16-12-52.png) +> IO 类的继承关系图 ![](2021-03-05-16-29-19.png) > 目录 @@ -13,10 +13,11 @@ ## 0 stream基础知识 -### IO对象没有拷贝或赋值 +> IO对象没有拷贝或赋值 ### 管理IO的状态 +> 继承自`basic_ios`和`basic_ios_base`基类 * 用来记录stream可能出现的状态。 ![](2021-03-05-19-15-08.png) @@ -68,27 +69,175 @@ cout< +``` +* 针对常用的字符提供了以下IO方式。也可以自己根据定义实现二进制IO的过程。 +``` +typedef basic_ios ios; +typedef basic_ios wios; + +typedef basic_streambuf streambuf; +typedef basic_streambuf wstreambuf; +typedef basic_filebuf filebuf; +typedef basic_filebuf wfilebuf; +typedef basic_stringbuf stringbuf; +typedef basic_stringbuf wstringbuf; +typedef basic_syncbuf syncbuf; +typedef basic_syncbuf wsyncbuf; + +typedef basic_istream istream; +typedef basic_istream wistream; +typedef basic_ostream ostream; +typedef basic_ostream wostream; +typedef basic_iostream iostream; +typedef basic_iostream wiostream; + +typedef basic_ifstream ifstream; +typedef basic_ifstream wifstream; +typedef basic_ofstream ofstream; +typedef basic_ofstream wofstream; +typedef basic_fstream fstream; +typedef basic_fstream wfstream; + +typedef basic_istringstream istringstream; +typedef basic_istringstream wistringstream; +typedef basic_ostringstream ostringstream; +typedef basic_ostringstream wostringstream; +typedef basic_stringstream stringstream; +typedef basic_stringstream wstringstream; + +typedef basic_osyncstream osyncstream; +typedef basic_osyncstream wosyncstream; +``` + +### 预定义标准流对象 + +定义于头文件 `` + +* cin\wcin从标准 C 输入流 stdin 中读取(全局对象) +* cout\wcout写入到标准 C 输出流 stdout(全局对象) +* cerr\wcerr写入到标准 C 错误流 stderr, 无缓冲(全局对象) +* clog\wclog写入到标准 C 错误流 stderr + +### 常用的istream方法 + +| 有格式输入 || +|---|---| +| operator>> | 提取带格式数据 (std::basic_istream 的公开成员函数) [编辑] | +| 无格式输入 | +| get | 从流中读并取走(移除类似指针向下一个元素移动)一个字符 (std::basic_istream 的公开成员函数) [编辑] | +| peek | 仅读出但不取走(不移除类似指针并未移动)一个字符 (std::basic_istream 的公开成员函数) [编辑] | +| unget | 撤销流中刚取走(移除,类似指针向后退回一个位置)的字符 (std::basic_istream 的公开成员函数) [编辑] | +| putback | 往输入流中退回一个字符 (std::basic_istream 的公开成员函数) [编辑] | +| getline | 一直读并取走字符,直至找到给定字符 (std::basic_istream 的公开成员函数) [编辑] | +| ignore | 读且取走并舍弃字符,直至发现给定字符 (std::basic_istream 的公开成员函数) [编辑] | +| read | 读并取走一块字符 (std::basic_istream 的公开成员函数) [编辑] | +| readsome | 读并取走已经可用的字符块 (std::basic_istream 的公开成员函数) [编辑] | +| gcount | 返回上次无格式输出操作所取走的字符数量 (std::basic_istream 的公开成员函数) [编辑] | +| 寻位 | +| tellg | 返回输入位置指示器 (std::basic_istream 的公开成员函数) [编辑] | +| seekg | 设置输入位置指示器 (std::basic_istream 的公开成员函数) [编辑] | +| 杂项 | +| sync | 与底层存储设备同步 (std::basic_istream 的公开成员函数) [编辑] | + + +### 常用的ostream方法 + +| 有格式输出 || +|---|---| +| operator<< | 插入带格式数据 (std::basic_ostream 的公开成员函数) [编辑] | +| 无格式输出 | +| put | 插入字符 (std::basic_ostream 的公开成员函数) [编辑] | +| write | 插入字符块 (std::basic_ostream 的公开成员函数) [编辑] | +| 寻位 | +| tellp | 返回输出位置指示器 (std::basic_ostream 的公开成员函数) [编辑] | +| seekp | 设置输出位置指示器 (std::basic_ostream 的公开成员函数) [编辑] | +| 杂项 | +| flush | 与底层存储设备同步 (std::basic_ostream 的公开成员函数) [编辑] | + + +### 输入方法 +* 绑定istream对象的输入方法 + * istream>> + * istream.get + * istream.getline +* 非对象绑定的输入方法 + * getline + * gets + * getchar +``` +getline(cin,str); + +char m[20]; +gets(m); //不能写成m=gets(); + +char ch; +ch=getchar(); //不能写成getchar(ch); +``` + ## 1 iostream +### 头文件 +``` +#include +``` +### iostream特有的方法 +> 全部继承自ios ios_base istream ostream ## 2 fstream +``` +#include +``` +### fstream特有方法 -### 文件流的方法 +文件操作 +* is_open检查流是否有关联文件 +* open打开文件,并将它与流关联 +* close关闭关联文件 ### 文件模式 ![](2021-03-05-19-42-28.png) ### 实例 +```C++ +//2. 创建流 +ofstream output; +//3. 打开文件,将流与文件相关联,这里使用相对路径 +output.open("number.txt"); +//4. 向文件写入数据 +output << 1 << " " << 2 << " " << 3 << endl; -## 3 sstream - -### string流 +//5. 关闭流 +output.close(); ``` - //string stream + + +## 3 stringstream + +### 头文件 +``` +#include + +stringstream basic_stringstream +wstringstream basic_stringstream +``` +### stringstream的操作 + +``` +str获取或设置底层字符串设备对象的内容 +``` +### string输入流 +``` + //string stream。字符串初始化输入流,通过输入流读取字符串 struct Person{ string name; string phone; @@ -105,10 +254,27 @@ cout<> format_string; +cout< +``` > 使用流操纵符完成格式化输入输出 | **流操纵算子** | **作  用** | diff --git a/C++/标准库/7.cpp b/C++/标准库/7.cpp index e2712d2c..c0e9b90d 100644 --- a/C++/标准库/7.cpp +++ b/C++/标准库/7.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; int main(){ @@ -26,20 +27,45 @@ int main(){ // 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< p; + // string line; + // if(getline(cin,line)){ + // Person pp; + // stringstream record(line); + // record>>pp.name; + // record>>pp.phone; + // p.push_back(pp); + // } + // cout<> format_string; + cout< +``` + +### 随机数概述 + +* 随机数分布。随机数的分布方式distribution +* 随机数引擎。产生随机数engin。随机性的源头 + +* 随机数生成器。由一个随机数引擎和一个随机数分布,组合成一个随机数生成器。 + + +### 随机数引擎的操作 +![](2021-03-07-21-14-33.png) +![](2021-03-08-14-04-34.png) +* 编译器会自动选择一个随机数引擎作为default_random_engine的类型的引擎。 +``` + default_random_engine e; + cout< u<0,9>; +default_random_engine e(323);//指定初始化的种子 +``` + +### 生成随机数的标准操作 + +``` +#include +#include + +#include +using namespace std; + +int main( ){ + default_random_engine e; + cout< u(0, 9); + for(int i=0; i<10; ++i) + cout<(up_bound,down_bound)均匀整数分布T:short int long longlong。指定均匀分布的最大最小值。 +* uniform_real_distribution(up_bound,down_bound)均匀浮点数分布T:float double.指定均匀分的的最大最小值 + +## 1.4 C的方法 + +### rand() +``` +rand()%100//在100中产生随机数, 但是因为没有随机种子所以,下一次运行也是这个数,因此就要引出srand +``` + +### srand() + +``` +srand((int)time(0)); // 产生随机种子把0换成NULL也行 +``` +## 2 复数 + +### 常用操作 + +![](2021-03-08-14-44-22.png) + + +### 数值访问 + +![](2021-03-08-14-44-45.png) + +### 算数运算 +![](2021-03-08-14-45-15.png) + +### 输入输出运算 + +![](2021-03-08-14-45-22.png) +### 超越函数 +![](2021-03-08-14-46-04.png) + + +## 3 全局数值函数 + +### 头文件 +``` +#include +#include +``` + +### 数值函数 + +![](2021-03-08-14-47-09.png) + + +## 4 Valarray 数值数组 \ No newline at end of file diff --git a/C++/标准库/8.cpp b/C++/标准库/8.cpp new file mode 100644 index 00000000..ad94dc4a --- /dev/null +++ b/C++/标准库/8.cpp @@ -0,0 +1,15 @@ +#include +#include + +#include +using namespace std; + +int main( ){ + default_random_engine e; + cout< u(0, 9); + for(int i=0; i<10; ++i) + cout<