随机数、时间、数值

This commit is contained in:
yinkanglong_lab
2021-03-08 14:49:54 +08:00
parent 60c42c4146
commit 3293878a33
19 changed files with 458 additions and 58 deletions

View File

@@ -12,6 +12,7 @@
"regex": "cpp",
"chrono": "cpp",
"iostream": "cpp",
"ostream": "cpp"
"ostream": "cpp",
"iomanip": "cpp"
}
}

View File

@@ -159,50 +159,95 @@ hours aDay(24);//表示一天的duration
```
#include<ctime>
```
### 概述
* 有四个与时间相关的类型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 <iostream>
#include <ctime>
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<random>
### 使用tm格式化当前时间
```C++
#include <iostream>
#include <ctime>
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<<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<>
```
## 10 C++的C兼容
## 8 C++的C兼容
### cstddef

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

View File

@@ -1,5 +1,8 @@
# 算法
> 参考文献
> * [C++进阶STL算法总结](https://www.jianshu.com/p/eb554b0943ab)
> * [C++标准库STL算法](https://www.cnblogs.com/larry-xia/p/9497340.html)
## 1 泛型算法概览

View File

@@ -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的正则匹配搜索算法。

View File

@@ -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<<unitbuf;//所有的输出操作后立即刷新缓冲区
cout<<nounitbuf;//回到正常的刷新方式
```
### 字符类型的IO预定义
* IO 类本质上都是模板。可以针对任何类型IO。
```
steam<type,traits>
```
* 针对常用的字符提供了以下IO方式。也可以自己根据定义实现二进制IO的过程。
```
typedef basic_ios<char> ios;
typedef basic_ios<wchar_t> wios;
typedef basic_streambuf<char> streambuf;
typedef basic_streambuf<wchar_t> wstreambuf;
typedef basic_filebuf<char> filebuf;
typedef basic_filebuf<wchar_t> wfilebuf;
typedef basic_stringbuf<char> stringbuf;
typedef basic_stringbuf<wchar_t> wstringbuf;
typedef basic_syncbuf<char> syncbuf;
typedef basic_syncbuf<wchar_t> wsyncbuf;
typedef basic_istream<char> istream;
typedef basic_istream<wchar_t> wistream;
typedef basic_ostream<char> ostream;
typedef basic_ostream<wchar_t> wostream;
typedef basic_iostream<char> iostream;
typedef basic_iostream<wchar_t> wiostream;
typedef basic_ifstream<char> ifstream;
typedef basic_ifstream<wchar_t> wifstream;
typedef basic_ofstream<char> ofstream;
typedef basic_ofstream<wchar_t> wofstream;
typedef basic_fstream<char> fstream;
typedef basic_fstream<wchar_t> wfstream;
typedef basic_istringstream<char> istringstream;
typedef basic_istringstream<wchar_t> wistringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_ostringstream<wchar_t> wostringstream;
typedef basic_stringstream<char> stringstream;
typedef basic_stringstream<wchar_t> wstringstream;
typedef basic_osyncstream<char> osyncstream;
typedef basic_osyncstream<wchar_t> wosyncstream;
```
### 预定义标准流对象
定义于头文件 `<iostream>`
* cin\wcin从标准 C 输入流 stdin 中读取(全局对象)
* cout\wcout写入到标准 C 输出流 stdout(全局对象)
* cerr\wcerr写入到标准 C 错误流 stderr 无缓冲(全局对象)
* clog\wclog写入到标准 C 错误流 stderr
### 常用的istream方法
| 有格式输入 ||
|---|---|
| operator>> | 提取带格式数据 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| 无格式输入 |
| get | 从流中读并取走(移除类似指针向下一个元素移动)一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| peek | 仅读出但不取走(不移除类似指针并未移动)一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| unget | 撤销流中刚取走(移除,类似指针向后退回一个位置)的字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| putback | 往输入流中退回一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| getline | 一直读并取走字符,直至找到给定字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| ignore | 读且取走并舍弃字符,直至发现给定字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| read | 读并取走一块字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| readsome | 读并取走已经可用的字符块 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| gcount | 返回上次无格式输出操作所取走的字符数量 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| 寻位 |
| tellg | 返回输入位置指示器 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| seekg | 设置输入位置指示器 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
| 杂项 |
| sync | 与底层存储设备同步 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
### 常用的ostream方法
| 有格式输出 ||
|---|---|
| operator<< | 插入带格式数据 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
| 无格式输出 |
| put | 插入字符 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
| write | 插入字符块 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
| 寻位 |
| tellp | 返回输出位置指示器 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
| seekp | 设置输出位置指示器 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
| 杂项 |
| flush | 与底层存储设备同步 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
### 输入方法
* 绑定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>
```
### iostream特有的方法
> 全部继承自ios ios_base istream ostream
## 2 fstream
```
#include<fstream>
```
### 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<sstream>
stringstream basic_stringstream<char>
wstringstream basic_stringstream<wchar_t>
```
### stringstream的操作
```
str获取或设置底层字符串设备对象的内容
```
### string输入流
```
//string stream。字符串初始化输入流通过输入流读取字符串
struct Person{
string name;
string phone;
@@ -105,10 +254,27 @@ cout<<nounitbuf;//回到正常的刷新方式
cout<<p[0].name<<endl;
cout<<p[0].phone<<endl;
```
### string输入输出流格式化字符
```C++
//string stream 对字符串进行格式化
double value = 4.55667;
string format_string;
stringstream sstream;
// 将int类型的值放入输入流中
sstream<<setw(10)<<setfill('0')<<fixed<<setprecision(3)<<value;
// 从sstream中抽取前面插入的int类型的值赋给string类型
sstream >> format_string;
cout<<format_string;
```
## 4 格式化输入输出
### 头文件
```
#include<iomanip>
```
> 使用流操纵符完成格式化输入输出
| **流操纵算子** | **作  用** |

View File

@@ -3,6 +3,7 @@
#include<sstream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int main(){
@@ -26,20 +27,45 @@ int main(){
// 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;
// //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;
//string stream 对字符串进行格式化
double value = 4.55667;
string format_string;
stringstream sstream;
// 将int类型的值放入输入流中
sstream<<setw(10)<<setfill('0')<<fixed<<setprecision(3)<<value;
// 从sstream中抽取前面插入的int类型的值赋给string类型
sstream >> format_string;
cout<<format_string;
//2. 创建流
ofstream output;
//3. 打开文件,将流与文件相关联,这里使用相对路径
output.open("number.txt");
//4. 向文件写入数据
output << 1 << " " << 2 << " " << 3 << endl;
//5. 关闭流
output.close();
return 0;
}

View File

@@ -0,0 +1,119 @@
# 数值方法
## 1.1 随机数
### 头文件
```
#include<random>
```
### 随机数概述
* 随机数分布。随机数的分布方式distribution
* 随机数引擎。产生随机数engin。随机性的源头
* 随机数生成器。由一个随机数引擎和一个随机数分布,组合成一个随机数生成器。
### 随机数引擎的操作
![](2021-03-07-21-14-33.png)
![](2021-03-08-14-04-34.png)
* 编译器会自动选择一个随机数引擎作为default_random_engine的类型的引擎。
```
default_random_engine e;
cout<<e()<<endl;
```
### 随机数分布的操作
![](2021-03-07-21-20-51.png)
![](2021-03-08-14-06-13.png)
* 需要给默认的随机数引擎选择一个分布序列。
```
uniform_int_distribution<unsigned> u<0,9>;
default_random_engine e(323);//指定初始化的种子
```
### 生成随机数的标准操作
```
#include <iostream>
#include <random>
#include<ctime>
using namespace std;
int main( ){
default_random_engine e;
cout<<time(NULL)<<endl;
e.seed(time(NULL));
uniform_int_distribution<unsigned> u(0, 9);
for(int i=0; i<10; ++i)
cout<<u(e)<<endl;
return 0;
}
```
## 1.2 随机分布引擎
C++提供了16个随机数引擎。
![](2021-03-08-14-03-18.png)
* default_random_engine
## 1.3 随机分布类型
C++提供了五大类随机分布
![](2021-03-08-14-05-20.png)
* uniform_int_distribution<T>(up_bound,down_bound)均匀整数分布T:short int long longlong。指定均匀分布的最大最小值。
* uniform_real_distribution<T>(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<cmath>
#include<cstdlib>
```
### 数值函数
![](2021-03-08-14-47-09.png)
## 4 Valarray 数值数组

15
C++/标准库/8.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <iostream>
#include <random>
#include<ctime>
using namespace std;
int main( ){
default_random_engine e;
cout<<time(nullptr)<<endl;
e.seed(time(nullptr));
uniform_int_distribution<unsigned> u(0, 9);
for(int i=0; i<10; ++i)
cout<<u(e)<<endl;
return 0;
}