Boost–progress_timer

简介:

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

image


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/26/2418275.html,如需转载请自行联系原作者
相关文章
|
2月前
|
编解码 安全 Linux
Clock sources, Clock events, sched_clock() and delay timers【ChatGPT】
Clock sources, Clock events, sched_clock() and delay timers【ChatGPT】
|
11月前
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
|
12月前
|
内存技术
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
72 0
|
C++
boost之signal的使用
boost是C++的一个扩展库,被称为C++准标准库,里面的组件很丰富,并且引用方便,85%的组件只需要引用头文件即可使用。在嵌入式系统也可以很方便的使用,这里介绍一下signal的使用,有点类似Qt里的信号槽。 可以接收静态函数、类成员函数、labmda表达式。 下面这个是使用signal封装的一个事件注册处理模板,使用起来还是很方便的。
132 0
|
Python
may have been in progress in another thread when fork() was called.
may have been in progress in another thread when fork() was called.
139 0
may have been in progress in another thread when fork() was called.
RxJava/RxAndroid:timer(long delay, TimeUnit unit)
RxJava/RxAndroid:timer(long delay, TimeUnit unit) timer起到定时器的作用,本例使用timer延迟3秒执行一个输出任务: package com.
1375 0
RxJava/RxAndroid:ConnectableObservable &amp; replay(int bufferSize, long time, TimeUnit unit)
RxJava/RxAndroid:ConnectableObservable & replay(int bufferSize, long time, TimeUnit unit) import android.
1062 0