Boost程序库完全开发指南阅读笔记(一) -- timer , progress_timer , progress_display

开始拜读《Boost程序库完全开发指南》一书



#include  //timer
#include  //progress_timer , progress_display

using namespace boost;

#include 
#include  // stringstream
#include  // Stop()
using namespace std;

//扩展progress_timer的计时精度

class new_progress_timer : public timer, private noncopyable
{

public:
	explicit new_progress_timer(std::ostream & os = std::cout)
		: timer(), noncopyable(), m_os(os) {}
	void set_precision(int precision)
	{
		this->precision = precision;
	}
	~new_progress_timer()
	{
		try
		{
			std::istream::fmtflags old_flags = m_os.setf(std::istream::fixed,
				std::istream::floatfield);
			std::streamsize old_prec = m_os.precision(precision);
			m_os << elapsed() << " s\n" // "s" is System International d'Unites std
				<< std::endl;
			m_os.flags(old_flags);
			m_os.precision(old_prec);
		}

		catch (...) {} // eat any exceptions
	} // ~progress_timer

private:
	std::ostream & m_os;
	int precision = 3;
};


int main()
{
	//timer
	/*
	timer t;//一旦声明构造函数启动计时
	cout << "max timespan :" << t.elapsed_max() / 3600 << "h" << endl; //可度量的最大时间
	cout << "min timespan :" << t.elapsed_min() << "s" << endl; //可度量的最小时间
	cout << "now time elapesd:" << t.elapsed() / 3600 << "s" << endl;//自从对象t创建已经流逝的时间
	
	int stop;
	cin >> stop;
	*/

	//progress_timer
	/*
	progress_timer t;//在析构时会自动输出已经流逝的时间 
	//cout << t.elapsed() << endl; //继承自timer,可以按照timer用
	//在同一个程序中测量多个时间用花括号限定生命周期
	{
		progress_timer t;
	}

	{
		progress_timer t;
	}
	//转移progress_timer的输出
	stringstream ss;
	{
		ss << "by stringstream :";
		progress_timer t(ss);
	}
	cout << ss.str() << endl;
	*/
	//扩展计时精度 -- 仿造progress_timer 从timer 继承
	/*
	{
		new_progress_timer t;
		t.set_precision(1);
	}

	{
		new_progress_timer t;
		t.set_precision(2);
	}

	{
		new_progress_timer t;
		t.set_precision(3);
	}

	{
		new_progress_timer t;
		t.set_precision(4);
	}

	{
		new_progress_timer t;
		t.set_precision(5);
	}

	int stop;
	cin >> stop;
	*/

	//在屏幕上显示进度条 -- 适用于progress_display和程序本身输出源不同的情况
	int cycle = 100;
	progress_display t(cycle);
	while (cycle--)
	{
		Sleep(100);
		t += 1;
	}
	int stop;
	cin >> stop;

}


你可能感兴趣的:(【C++】,【Boost】)