progress_timer.cpp
#include <iostream>
#include <boost/progress.hpp>
#include <boost/static_assert.hpp>
#include <vector>
using namespace boost;
using namespace std;
template<int N=2>
class new_progress_timer:public progress_timer
{
public:
new_progress_timer(std::ostream &os = std::cout):m_os(os)
{
BOOST_STATIC_ASSERT(N >= 0 && N <= 10);
}
~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(N);
m_os << elapsed() << "s \n"
<< std::endl;
m_os.flags( old_flags);
m_os.precision(old_prec);
}
catch(...){}
}
private:
std::ostream &m_os;
};
template<>
class new_progress_timer<2>:public boost::progress_timer{};
int main()
{
new_progress_timer<10> t;
//system("pause");
}
progress_display.cpp
#include <iostream>
#include <string>
#include <boost/progress.hpp>
#include <boost/static_assert.hpp>
#include <vector>
using namespace boost;
using namespace std;
int main()
{
vector<string> v(1000000);
progress_display pd(v.size(),cout,"hello","ajioy"," prog");
vector<string>::iterator pos;
for(pos = v.begin(); pos != v.end(); ++pos)
{
*pos = 'a';
++pd;
}
//system("pause");
}
运行结果:
hello0% 10 20 30 40 50 60 70 80 90 100%
ajioy|----|----|----|----|----|----|----|----|----|----|
prog***************************************************