#include <boost\timer.hpp>
#include <boost\progress.hpp>
一、timer类函数
// timer类的示例。 void Lib_Demo_timer::Demo_timer() { timer t; cout << "可度量的最大单位:" << t.elapsed_max() / 3600 << "小时" << endl; cout << "可度量的最小单位:" << t.elapsed_min() << "s" << endl; cout << "计时开始...按任意键计时" << endl; system("pause"); cout << "已通过的时间:" << t.elapsed() << "s" << endl; }
输出:spa
可度量的最大单位:596.523小时
可度量的最小单位:0.001s
计时开始...按任意键计时
请按任意键继续. . .
已通过的时间:0.74s
请按任意键继续. . .操作系统
二、process类code
// progress类的示例。 void Lib_Demo_timer::Demo_process(void) { { boost::progress_timer t; cout << "须要计时的代码块1" << endl; system("pause"); } stringstream ss; { boost::progress_timer t(ss); cout << "须要计时的代码块2" << endl; system("pause"); } cout << ss.str() << endl; }
输出:对象
须要计时的代码块1
请按任意键继续. . .
0.96 sblog
须要计时的代码块2
请按任意键继续. . .
1.66 sci
请按任意键继续. . .字符串
三、progress_display类generator
// progress_display类的示例。 void Lib_Demo_timer::Demo_progress_display(void) { vector<string> v(100); progress_display pd(v.size()); for(vector<string>::const_iterator i = v.begin(); i != v.end(); ++i) { //针对i的处理 Sleep(100); ++pd; } }
输出:源码
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
请按任意键继续. . .
一、引用库的方式
(1)、包含源码的方式:经过启用宏 BOOST_DATE_TIME_SOURCE 等。
#define BOOST_DATE_TIME_SOURCE #define BOOST_DATE_TIME_NO_LIB #include <libs/date_time/src/gregorian/greg_names.hpp> #include <libs/date_time/src/gregorian/date_generators.cpp> #include <libs/date_time/src/gregorian/greg_month.cpp> #include <libs/date_time/src/gregorian/greg_weekday.cpp> #include <libs/date_time/src/gregorian/gregorian_types.cpp>
注意:若是使用的boost版本较高,那么在使用(较低版本)STLport标准库时会编译错误,解决方法是使用VC的标准库。
(2)、包含已编译库的方式
#include <boost/date_time/gregorian/gregorian.hpp>
须要在项目配置的“库目录”中增长boost编译库的路径。
二、定义日期对象 date
头文件
#include <boost/date_time/gregorian/gregorian.hpp>
// date 初始化示例 boost::gregorian::date d1; //默认建立一个 无效日期 //宏 DATE_TIME_NO_DEFAULT_CONSTRUCTOR 能够禁用 构造无效日期的 默认构造函数 date d2(2010, 1, 1); date d3(2000, Jan, 1); //使用英文指定月份 date d4(d2); assert(d1 == date(boost::date_time::special_values::not_a_date_time)); assert(d2 == d4); //date支持比较操做 assert(d3 < d4);
//从字符串建立 //注意:包含源码是依赖 #include <libs/date_time/src/gregorian/greg_month.cpp> date d1_1 = boost::gregorian::from_string("1999-12-31"); date d2_1(boost::gregorian::from_string("2005/1/1")); date d3_1 = boost::gregorian::from_undelimited_string("20011118");
//使用 day_clock 建立,返回当天日期对象,注意:依赖操做系统的时区设置 cout << boost::gregorian::day_clock::local_day() << endl; cout << day_clock::local_day_ymd << endl; cout << day_clock::universal_day() << endl; cout << day_clock::universal_day_ymd << endl;
//建立特殊日期 date d1_2(special_values::neg_infin); //负无限日期 date d2_2(special_values::pos_infin); //正无限日期 date d3_2(special_values::not_a_date_time); //无效日期 date d4_2(special_values::max_date_time); //最大可能日期9999-12-31 date d5_2(special_values::min_date_time); //最小可能日期1400-01-01
//异常建立,boost会抛出异常。 date d1_3(1399, 12, 1); date d2_3(10000, 1, 1); date d3_3(2010, 2, 29);