参考:

1. https://baike.baidu.com/item/stdio.h

2. https://www.cnblogs.com/jwk000/p/3560086.html

1. stdio.h :

stdio 就是指 “standard input & output"(标准输入输出

所以,源代码中如用到标准输入输出函数时,就要包含这个头文件
例如c语言中的 printf("%d",i); scanf("%d",&i);等函数。 [1] 
 
int getchar()//从标准输入设备写入一个字符
int putchar()//向标准输出设备读出一个字符
int scanf(char*format[,argument…])//从标准输入设备读入格式化后的数据
int printf(char*format[,argument…])//向标准输出设备输出格式化字符串
chargets(char*string)//从标准输入设备读入一个字符串
int puts(char*string)//向标准输出设备输出一个字符串
int sprintf(char*string,char*format[,…])//把格式化的数据写入某个字符串缓冲区
 
2.#include <chrono>:
C++11 std::chrono库详解

 

  所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解。

  chrono是一个time library, 源于boost,现在已经是C++标准。话说今年似乎又要出新标准了,好期待啊!

  要使用chrono库,需要#include<chrono>,其所有实现均在std::chrono namespace下。注意标准库里面的每个命名空间代表了一个独立的概念。所以下文中的概念均以命名空间的名字表示! chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock

 
1.Durations
std::chrono::duration 表示一段时间,比如两个小时,12.88秒,半个时辰,一炷香的时间等等,只要能换算成秒即可。
1 template <class Rep, class Period = ratio<1> > class duration;
其中
Rep表示一种数值类型,用来表示Period的数量,比如int float double
Period是ratio类型,用来表示【用秒表示的时间单位】比如second milisecond
常用的duration<Rep,Period>已经定义好了,在std::chrono::duration下:
ratio<3600, 1>                hours
ratio<60, 1>                    minutes
ratio<1, 1>                      seconds
ratio<1, 1000>               microseconds
ratio<1, 1000000>         microseconds
ratio<1, 1000000000>    nanosecons
 
这里需要说明一下ratio这个类模版的原型:
1 template <intmax_t N, intmax_t D = 1> class ratio;
N代表分子,D代表分母,所以ratio表示一个分数值。
注意,我们自己可以定义Period,比如ratio<1, -2>表示单位时间是-0.5秒。
 
由于各种duration表示不同,chrono库提供了duration_cast类型转换函数。
1 template <class ToDuration, class Rep, class Period>
2 constexpr ToDuration duration_cast (const duration<Rep,Period>& dtn);
典型的用法是表示一段时间:
 
 1 // duration constructor
2 #include <iostream>
3 #include <ratio>
4 #include <chrono>
5
6 int main ()
7 {
8 typedef std::chrono::duration<int> seconds_type;
9 typedef std::chrono::duration<int,std::milli> milliseconds_type;
10 typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;
11
12 hours_type h_oneday (24); // 24h
13 seconds_type s_oneday (60*60*24); // 86400s
14 milliseconds_type ms_oneday (s_oneday); // 86400000ms
15
16 seconds_type s_onehour (60*60); // 3600s
17 //hours_type h_onehour (s_onehour); // NOT VALID (type truncates), use:
18 hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));
19 milliseconds_type ms_onehour (s_onehour); // 3600000ms (ok, no type truncation)
20
21 std::cout << ms_onehour.count() << "ms in 1h" << std::endl;
22
23 return 0;
24 }
25
26 duration还有一个成员函数count()返回Rep类型的Period数量,看代码:
27
28 // duration::count
29 #include <iostream> // std::cout
30 #include <chrono> // std::chrono::seconds, std::chrono::milliseconds
31 // std::chrono::duration_cast
32
33 int main ()
34 {
35 using namespace std::chrono;
36 // std::chrono::milliseconds is an instatiation of std::chrono::duration:
37 milliseconds foo (1000); // 1 second
38 foo*=60;
39
40 std::cout << "duration (in periods): ";
41 std::cout << foo.count() << " milliseconds.\n";
42
43 std::cout << "duration (in seconds): ";
44 std::cout << foo.count() * milliseconds::period::num / milliseconds::period::den;
45 std::cout << " seconds.\n";
46
47 return 0;
48 }
 
2.Time points
std::chrono::time_point 表示一个具体时间,如上个世纪80年代、你的生日、今天下午、火车出发时间等,只要它能用计算机时钟表示。鉴于我们使用时间的情景不同,这个time point具体到什么程度,由选用的单位决定。一个time point必须有一个clock计时。参见clock的说明。
 
1 template <class Clock, class Duration = typename Clock::duration>  class time_point;
 
下面是构造使用time_point的例子:
 1 // time_point constructors
2 #include <iostream>
3 #include <chrono>
4 #include <ctime>
5
6 int main ()
7 {
8 using namespace std::chrono;
9
10 system_clock::time_point tp_epoch; // epoch value
11
12 time_point <system_clock,duration<int>> tp_seconds (duration<int>(1));
13
14 system_clock::time_point tp (tp_seconds);
15
16 std::cout << "1 second since system_clock epoch = ";
17 std::cout << tp.time_since_epoch().count();
18 std::cout << " system_clock periods." << std::endl;
19
20 // display time_point:
21 std::time_t tt = system_clock::to_time_t(tp);
22 std::cout << "time_point tp is: " << ctime(&tt);
23
24 return 0;
25 }
26
time_point有一个函数time_from_eproch()用来获得1970年1月1日到time_point时间经过的duration。
举个例子,如果timepoint以天为单位,函数返回的duration就以天为单位。
 
由于各种time_point表示方式不同,chrono也提供了相应的转换函数 time_point_cast。
1 template <class ToDuration, class Clock, class Duration>
2 time_point<Clock,ToDuration> time_point_cast (const time_point<Clock,Duration>& tp);
比如计算
/

 1 / time_point_cast
2 #include <iostream>
3 #include <ratio>
4 #include <chrono>
5
6 int main ()
7 {
8 using namespace std::chrono;
9
10 typedef duration<int,std::ratio<60*60*24>> days_type;
11
12 time_point<system_clock,days_type> today = time_point_cast<days_type>(system_clock::now());
13
14 std::cout << today.time_since_epoch().count() << " days since epoch" << std::endl;
15
16 return 0;
17 }
3.Clocks
 
std::chrono::system_clock 它表示当前的系统时钟,系统中运行的所有进程使用now()得到的时间是一致的。
每一个clock类中都有确定的time_point, duration, Rep, Period类型。
操作有:
now() 当前时间time_point
to_time_t() time_point转换成time_t秒
from_time_t() 从time_t转换成time_point
典型的应用是计算时间日期:
 1 // system_clock example
2 #include <iostream>
3 #include <ctime>
4 #include <ratio>
5 #include <chrono>
6
7 int main ()
8 {
9 using std::chrono::system_clock;
10
11 std::chrono::duration<int,std::ratio<60*60*24> > one_day (1);
12
13 system_clock::time_point today = system_clock::now();
14 system_clock::time_point tomorrow = today + one_day;
15
16 std::time_t tt;
17
18 tt = system_clock::to_time_t ( today );
19 std::cout << "today is: " << ctime(&tt);
20
21 tt = system_clock::to_time_t ( tomorrow );
22 std::cout << "tomorrow will be: " << ctime(&tt);
23
24 return 0;
25 }
26
std::chrono::steady_clock 为了表示稳定的时间间隔,后一次调用now()得到的时间总是比前一次的值大(这句话的意思其实是,如果中途修改了系统时间,也不影响now()的结果),每次tick都保证过了稳定的时间间隔。
操作有:
now() 获取当前时钟
典型的应用是给算法计时:
 1 // steady_clock example
2 #include <iostream>
3 #include <ctime>
4 #include <ratio>
5 #include <chrono>
6
7 int main ()
8 {
9 using namespace std::chrono;
10
11 steady_clock::time_point t1 = steady_clock::now();
12
13 std::cout << "printing out 1000 stars...\n";
14 for (int i=0; i<1000; ++i) std::cout << "*";
15 std::cout << std::endl;
16
17 steady_clock::time_point t2 = steady_clock::now();
18
19 duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
20
21 std::cout << "It took me " << time_span.count() << " seconds.";
22 std::cout << std::endl;
23
24 return 0;
25 }
26
最后一个时钟,std::chrono::high_resolution_clock 顾名思义,这是系统可用的最高精度的时钟。实际上high_resolution_clock只不过是system_clock或者steady_clock的typedef。
操作有:
now() 获取当前时钟。

chrono库还有几个小特性,但是像这种工具库,本着够用则已的态度,就不求全责备了。
(全文完)
 

  

C++中头文件简介(stdio.h & chrono)的更多相关文章

  1. C头文件之<stdio.h>

    (stdio.h) 该头文件主要是执行输入输出操作.文件中重要的概念是“流”(streams).“流”在函数库中用FILE表示,用指针类型FILE *来操作.有三个标准流:stdin, stdout, ...

  2. C++ 中头文件<bits/stdc++.h>的优缺点

    在编程竞赛中,我们常见一个头文件: #include <bits/stdc++.h> 发现它是部分C++中支持的一个几乎万能的头文件,包含所有的可用到的C++库函数,如<istrea ...

  3. C/C++ 引入头文件时 #include<***.h> 与 #include"***.h" 区别

    两种情况区分: 1.#include <> 编译器只会去系统文件目录中查找,找不到就报错. 2.#include " "  编译器会先在用户目录中查找,再到编译器设定的 ...

  4. C语言中头文件<stdio.h>中的#ifndef _STDIO_H_

    先了解这里的相关知识:http://www.cnblogs.com/stemon/p/4000468.html 头文件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都in ...

  5. Visual Studio中头文件stdafx.h的作用

    在较新版的Visual Studio中,新生成的C++项目文件的的头文件夹下会默认有头文件stdafx.h,而源文件夹下则默认有源文件stdafx.cpp,手动将这些文件删除后,编译时系统还会报错.下 ...

  6. c中头文件在cpp文件里引用和.h文件引用的思考

    我们在编敲代码中头文件是常常使用的. 可是头文件是应该包括在.H文件里还是在.cpp文件里.在这个其中有什么样去差别呢. 假如说我们编写了一个a.cpp  .我们将a.cpp文件的变量和函数申明在a. ...

  7. C++ 中头文件(.h)和源文件(.cc)的写法简述

    用C++编写比较大型的项目时,文件的分割管理确实确实是非常必要的 .下面就非常简洁明了地谈谈头文件(.h)和源文件(.cc)应该怎么写. 头文件(.h):写类的声明(包括类里面的成员和方法的声明).函 ...

  8. C++中头文件、源文件之间的区别与联系

    .h头文件和.cpp文件的区别 疑惑1:.h文件能够编写main函数吗? 实验: 编写test.h文件,里面包含main函数 若直接编译g++ test.h -o test,通过file命令 file ...

  9. C++万能头文件<bits/stdc++.h>的内容与优缺点

    最近发现了一个C++的头文件bits/stdc++.h,听说这是一个几乎包含了所有C++库函数的头文件,就想更深入的了解一下,下面是头文件内容 // C++ includes used for pre ...

随机推荐

  1. asterisk 传真服务器配置

    摘要: asterisk 可以作为电子传真服务器,进行收发电子传真.但是配置起来,比较麻烦,需要一番折腾.在这儿分享一下电子传真的配置,希望对朋友们有所帮助. 正题: asterisk 如果需要收发电 ...

  2. 浅谈 FTP、FTPS 与 SFTP

    无论是网盘还是云存储,上传都是一项很简单的操作.那些便捷好用的上传整理工具所用的 FTP 协议到底是什么意义,繁杂的模式又有何区别? 二狗子最近搭建了一个图片分享网站,每天都有好多人在他的网站上传许多 ...

  3. 【Android】AndroidStudio关于EventBus报错解决方法its super classes have no public methods with the @Subscribe

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先说明,以前我用eventBus的 ...

  4. vue中使用柱状图

    子组件 <template> <div> <div id="myChart" :style="{width: '400px', height ...

  5. 轻轻松松学CSS:媒体查询

    轻轻松松学CSS:利用媒体查询创建响应式布局 媒体查询,针对不同的媒体类型定制不同的样式规则.在网站开发中,可以创建响应式布局. 一.初步认识媒体查询在响应式布局中的应用 下面实例在屏幕可视窗口尺寸大 ...

  6. 聊聊redis单线程为什么能做到高性能和io多路复用到底是个什么鬼

    1:io多路复用epoll  io多路复用简单来说就是一个线程处理多个网络请求 我们知道epoll in 的事件触发是可读了,这个比较好理解,比如一个连接过来,或者一个数据发送过来了,那么in事件就触 ...

  7. RGB打水印在YUV图片上

    一. 概述 将RGB图片打在YUV上需要注意的是, 字体之外应该透明, 否则背景也会被覆盖不好看,  所以RGB必须有透明度,  本测试格式为BMP ARGB8888(也即B是最低字节, A是最高字节 ...

  8. Linux:系统用户和用户组

    一.用户介绍 用户分为三类,超级用户.虚拟用户.普通用户:系统通过用户的uid识别用户:超级用户uid=0,虚拟用户uid=1-599,普通用户的uid=500-65535 用户和组相关配置文件/et ...

  9. python模块之----subprocess

    例子 >>> subprocess.getstatusoutput('pwd')(0, '/home/ronny')>>> subprocess.getoutput ...

  10. django之models报错

    django 执行python manage.py makemigrations报错:TypeError: __init__() missing 1 required positional argum ...