• 扩展progress_timer的计时精度


     progress对外输出精度只有小数点后两位(这点可以运行上节程序进行验证),即精确到0.01秒。

    我们使用模板技术仿造一个progress_timer编写一个新类:new_progress_timer,以实现任意精度的输出。

     new_progress_timer同样继承自timer,只是编程了模板类。模板参数N指明了输出精度,默认值为2,与progress_timer相同。

    #include <boost	imer.hpp>
    #include  <boostprogress.hpp>
    #include <booststatic_assert.hpp>
    #include <iostream>
    using namespace boost;
    using namespace std;
    
    //使用模板参数实现progress_timer
    template<int N=2>
    class new_progress_timer:public boost::timer
    {
    public:
        new_progress_timer(std::ostream &os=std::cout):m_os(os)
        {
            BOOST_STATIC_ASSERT(N>=0&&N<=10);
        }
    
        ~new_progress_timer(void)
        {
            try{
                //保持流的状态
                std::istream::fmtflags old_flags=m_os.setf(std::istream::fixed,std::istream::floatfield);
    
                std::streamsize old_prec=m_os.precision(N);
    
                m_os<<elapsed()<<"s
    "<<std::endl;
    
                m_os.flags(old_flags);
                m_os.precision(old_prec);
    
            }
            catch( ...)
            {
            }
        }
    
    private:
        std::ostream &m_os;
    };
    
    //当精度为2时,使用下面这个
    
    template<>
    class new_progress_timer<2>:public boost::progress_timer
    {};
    
    int main()
    {
         new_progress_timer<10>     t;  //声明一个计时器,开始计时
       //dosomething
        for(int i=0;i<100;i++)
        {
            cout<<"a";
        }
        cout<<endl;
    }
  • 相关阅读:
    php趣题小记
    git常用语法
    Go 使用小记
    小程序 显示对话框 确定-取消
    小程序 后退一页
    Vue脚手架使用步骤 2.9.6版本
    小程序 后台返回的对象数组(每个数组添加一个新的属性值)
    小程序 添加对象
    小程序图表wx-chart
    微信小程序之自定义select下拉选项框组件
  • 原文地址:https://www.cnblogs.com/zzu-liulei/p/6081673.html
Copyright © 2020-2023  润新知