Boost基础例子

来源:互联网 发布:立式数控车床编程 编辑:程序博客网 时间:2024/06/13 01:57

在vs2013中,环境配置参考其它blog

基础类型转换与定义

#include "stdafx.h"

#include <boost/lexical_cast.hpp>     
#include <iostream>     
using namespace std;
int mainx()
{
using boost::lexical_cast;
int a = lexical_cast<int>("123");
double b = lexical_cast<double>("123.0123456789");
string s0 = lexical_cast<string>(a);
string s1 = lexical_cast<string>(b);
cout << "number: " << a << "  " << b << endl;
cout << "string: " << s0 << "  " << s1 << endl;
int c = 0;
try{
c = lexical_cast<int>("abcd");
}
catch (boost::bad_lexical_cast& e){
cout << e.what() << endl;
}
return 0;

}


timer计时

void timerdemo(){boost::timer t;long long sum = 0;for (int i = 0; i < 1000000; i++)sum = sum + i;cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl;cout << "min timespan: " << t.elapsed_min() << "s"<<endl;//用时cout << "now time elapsed: " << t.elapsed() << "s" << endl;}


进度条例子

void progress_display_demo(){std::vector<string> v(100000);std::ofstream fs("c:\\log4j-2016-02-18.log");progress_display pd(v.size());std::vector<string>::iterator pos;for (pos = v.begin(); pos != v.end(); ++pos){fs << *pos << endl;++pd;}}




0 0
原创粉丝点击