珍惜时间

导航

 

●timer库

#include <boost\timer.hpp>
#include <boost\progress.hpp>

1、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;
}

输出:

可度量的最大单位:596.523小时
可度量的最小单位:0.001s
计时开始...按任意键计时
请按任意键继续. . .
已经过的时间:0.74s
请按任意键继续. . .

2、process类

// 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 s

需要计时的代码块2
请按任意键继续. . .
1.66 s


请按任意键继续. . .

3、progress_display类

// 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%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
请按任意键继续. . .

●date_time库

1、引用库的方式

(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编译库的路径。

2、定义日期对象 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);
posted on 2014-11-24 08:20  起点  阅读(301)  评论(0编辑  收藏  举报