博客
关于我
BOOST库 学习参考完全开发指南
阅读量:595 次
发布时间:2019-03-12

本文共 4289 字,大约阅读时间需要 14 分钟。

转载:

安装和配置

我的做法是,到下载对应的的boost的库,然后执行./bootstrap.sh./b2 install安装还之后,boost的头文件存放在在/usr/local/include,库文件存放在/usr/local/lib
hpp的含义就是,在头文件中,将函数的具体内容已经定义了。
安装完成之后输入测试代码

#include
#include
#include
using namespace std;int main(){ std::cout<
<

然后输入gcc -o a.out hello.cpp利用gcc进行编译,有如下报错:

/tmp/ccmH3TIR.o: In function `main':hello.cpp:(.text+0xa): undefined reference to `std::cout'hello.cpp:(.text+0xf): undefined reference to `std::ostream::operator<<(int)'hello.cpp:(.text+0x14): undefined reference to `std::basic_ostream
>& std::endl
>(std::basic_ostream
>&)'hello.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'hello.cpp:(.text+0x26): undefined reference to `std::cout'

应该用g++进行编译g++ -o a.out hello.cpp -Istdc++,然后运行./a.out输出105400 1_54,基本上就配置完成了。

timer库
包含三个重要的组件:计时器timer,progress_timer和进度指示器progress_display

#include
#include
using namespace boost;int main(){timer t;//可度量的最大时间,单位小时std::cout<

输出结果

2.56205e+09h1e-06snow time:0.000194

为了方便和学习,决定还是在vs下配置。点击这里查看

progress_timer处理时间

#include
#include
int main(){ boost::progress_timer t;//开始计时 std::cout << t.elapsed() << std::endl;//输出经历的时间 std::cin.get(); return 0;}

progress_display显示处理进度

创建日期对象

处理日期的组件

#include
using namespace boost::gregorian;

处理时间的组件

#include
using namespace boost::posix_time;
创建时间的对象
boost::gregorian::date d1;//实例化一个空的    boost::gregorian::date d2(2000, Jan, 1);    boost::gregorian::date d3(d2);//拷贝构造函数    boost::gregorian::date d4(2000, 1, 1);    //也可以通过一个字符串    boost::gregorian::date d5 = from_string("1999-12-31");    boost::gregorian::date d6 = from_string("2011/1/1");    boost::gregorian::date d7 = from_undelimited_string("20120101");

编译的时候遇到问题:

fatal error LNK1104: 无法打开文件“libboost_date_time-vc120-mt-gd-1_65.lib    1

解决办法:

内存管理

智能指针std::auto_ptr在离开作用域之后,智能指针会自动释放内存。但是也存在一些问题。所以 boost.smart_ptr库提供了更好的指针。boost.smart_ptr库提供了六种指针scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr、和intrusive_ptr 在使用的时候,只需要包含#include<boost/smart_ptr.hpp>并且使用boost命名空间就可以了。
scoped_ptr,特点,是允许在本作用域使用。不可以拷贝和赋值。

#include
#include
#include
int main(){ boost::scoped_ptr
sp(new std::string("txt")); std::cout << "sp: " << *sp << std::endl;//输出txt std::cout << "sp size:" << sp->size() << std::endl;//输出3 std::cin.get();}

shared_ptr是最有价值的部分

#include
#include
#include
#include
int main(){ boost::shared_ptr
sp(new int(10)); assert(sp.unique());//不需要加std。只需要包含assert.h头文件就可以。sp.unique来判断sp是否唯一。如果不唯一。立刻终止程序 boost::shared_ptr
sp2 = sp;//进行拷贝构造函数 *sp2 = 100; std::cout << *sp << std::endl;//拷贝构造函数修改,原始的指向也修改 std::cin.get();}

使用工厂函数进行批量赋值

#include 
#include
#include
#include
int main(){ auto sp = boost::make_shared
("hi david"); auto spv = boost::make_shared
>(10, 2); std::cin.get();}

对一个容器当中的数据进行遍历,并最这些数据进行输出

#include 
#include
#include
#include
#include
//定义一个向量用来typedef std::vector
> vs;int main(){ //声明一个拥有10个元素的vector vs v(10); int i = 0; for (auto pos = v.begin(); pos != v.end();++pos) { //利用工厂函数进行赋值 (*pos) = boost::make_shared
(++i); std::cout << *(*pos) << std::endl; }}

工厂函数

#include 
#include
#include
#include
#include
//定义接口类class abstract{public: virtual void f() = 0;//定义接口类 virtual void g() = 0;protected: virtual ~abstract() = default;//将析构函数定义成保护类型,除了他和他的子类,别人无权调用};class impl:public abstract{public: impl ()= default;// virtual ~impl() = default;public: virtual void f() { std::cout << "class impl f" << std::endl; } virtual void g() { std::cout << "class impl g" << std::endl; }};boost::shared_ptr
create(){ return boost::make_shared
();};int main(){ //p的数据类型是一个指针。 auto p = create();//工厂函数创建对象 p->f(); p->g(); std::cin.get(); return 0;} noncopy允许程序轻松实现一个禁止拷贝的类
你可能感兴趣的文章
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_说明操作步骤---大数据之Nifi工作笔记0028
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002---大数据之Nifi工作笔记0069
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>