progress_timer继承自timer,但是精度为2,为了扩展精度,我们自己实现一个类,代码如下:

#include <iostream> #include<boost/progress.hpp> #include <boost/static_assert.hpp> using namespace std; using namespace boost; template<int N=2> class new_progress_timer :public timer{ public: new_progress_timer(ostream &os=cout):m_os(os){ BOOST_STATIC_ASSERT(N>=0&&NULL<=10); } ~new_progress_timer(){ try{ istream::fmtflags old_flags=m_os.setf(istream::fixed,istream::floatfield); streamsize old_prec=m_os.precision(N); //输出时间 m_os<<elapsed()<<"s\n"<<endl; //恢复流的状态 m_os.flags(old_flags); m_os.precision(old_prec); }catch(...){ //do nothing } } private: ostream &m_os; }; int _tmain(int argc, _TCHAR* argv[]) { new_progress_timer<10> t; //cout<<t.elapsed(); 这里不需要亲自调用,当超过t的作用域的时候自动调用 return 0; }

Boost–progress_timer

相关文章:

  • 2021-11-14
  • 2021-11-01
  • 2021-08-13
  • 2021-04-07
  • 2022-02-04
  • 2022-12-23
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2021-06-11
  • 2021-07-24
  • 2021-11-01
  • 2022-12-23
  • 2021-07-21
相关资源
相似解决方案