博客
关于我
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允许程序轻松实现一个禁止拷贝的类
你可能感兴趣的文章
Necurs僵尸网络攻击美国金融机构 利用Trickbot银行木马窃取账户信息和欺诈
查看>>
Needle in a haystack: efficient storage of billions of photos 【转】
查看>>
NeHe OpenGL教程 07 纹理过滤、应用光照
查看>>
NeHe OpenGL教程 第四十四课:3D光晕
查看>>
Neighbor2Neighbor 开源项目教程
查看>>
neo4j图形数据库Java应用
查看>>
Neo4j图数据库_web页面关闭登录实现免登陆访问_常用的cypher语句_删除_查询_创建关系图谱---Neo4j图数据库工作笔记0013
查看>>
Neo4j图数据库的介绍_图数据库结构_节点_关系_属性_数据---Neo4j图数据库工作笔记0001
查看>>
Neo4j图数据库的数据模型_包括节点_属性_数据_关系---Neo4j图数据库工作笔记0002
查看>>
Neo4j安装部署及使用
查看>>
Neo4j电影关系图Cypher
查看>>
Neo4j的安装与使用
查看>>
Neo4j(1):图数据库Neo4j介绍
查看>>
Neo4j(2):环境搭建
查看>>
Neo4j(3):Neo4j Desktop安装
查看>>
Neo4j(4):Neo4j - CQL使用
查看>>
Neo图数据库与python交互
查看>>
NEO改进协议提案1(NEP-1)
查看>>
Neo私链
查看>>
NervanaGPU 项目使用教程
查看>>