您的位置:首页 > 编程语言 > C语言/C++

c++的boost库学习笔记

2015-04-18 08:21 357 查看
c++的boost库学习
boost和stlport编译,编译过程好麻烦,根据网上教程和boost完全开发指南,加自己摸索才勉强编译完成,做个笔记总结一下,具体编译方法,暂且不写

1,timer类,用于类似性能测试等计算时间。

下面代码是线程的helloworld和timer类的使用例子

#include<iostream>

#include<boost/thread/thread.hpp>

#include<boost\timer.hpp>

using namespace std;

using namespace boost;

void hello(){

cout<<"ddd"<<endl;

}

int main(){

timer t;

boost::thread thrd(&hello);

thrd.join();

cout<<t.elapsed_max()/3600<<endl;//最大时间,以小时为单位

cout<<t.elapsed_min()<<endl;//最小统计时间,以秒为单位

cout<<t.elapsed()<<endl;//建立对象开始,时间的流逝

}

2,progress_timer类,继承至timer类,一个好处是析构函数直接打印时间流逝

例子如下

#include<iostream>

#include<boost/thread/thread.hpp>

#include<boost\timer.hpp>

#include<boost\progress.hpp>

using namespace std;

using namespace boost;

void hello(){

cout<<"ddd"<<endl;

}

int main(){

timer t;

boost::thread thrd(&hello);

thrd.join();

cout<<t.elapsed_max()/3600<<endl;

cout<<t.elapsed_min()<<endl;

cout<<t.elapsed()<<endl;

progress_timer pt;

}

结果析构函数打印出0.00 s

缺点就是win32只能精确到毫秒,linux只能精确到微秒

3,progress_display进度条类,与上面两个类没关系

#include<iostream>

#include<boost\progress.hpp>

#include<vector>

#include<fstream>

using namespace std;

using namespace boost;

int main(){

vector<string> v(10000);

ofstream os("C:\\Users\\admin\\Desktop\\boostday2.txt");

progress_display pd(v.size());

vector<string>::iterator pos;

for(pos = v.begin();pos!=v.end();++pos){

os<<*pos<<endl;

++pd;

}

return 0;

}

缺点是,要保证程序不能有任何可能的输出,会打乱进度条的显示,那么进度条就没用了,但是它和progress_timer一样私有继承了noncopyable,防止被拷贝。

4,date_time类,可以满足绝大多数程序对时间的要求,但是不能处理1400年以前的时间,但是它的优点是很明显的,它允许我们更加自由地去操纵时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: