博客
关于我
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允许程序轻松实现一个禁止拷贝的类
你可能感兴趣的文章
Nginx 多端口配置和访问异常问题的排查与优化
查看>>
Nginx 如何代理转发传递真实 ip 地址?
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的 SSL 模块安装
查看>>
Nginx 的优化思路,并解析网站防盗链
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 相关介绍(Nginx是什么?能干嘛?)
查看>>
Nginx 知识点一网打尽:动静分离、压缩、缓存、跨域、高可用、性能优化...
查看>>
nginx 禁止以ip形式访问服务器
查看>>
NGINX 端口负载均衡
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>