BOOST库 学习参考完全开发指南(更新完毕)

     @DavidHan ,
     http://blog.csdn.net/david_han008/article/details/72811897

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

 #include<boost/version.hpp>
 #include<boost/config.hpp>
 #include<iostream>
using namespace std;
int main()
{
   std::cout<<BOOST_VERSION<<std::endl;
   std::cout<<BOOST_LIB_VERSION<<std::endl;
   return 0;
}

然后输入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<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
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<boost/timer.hpp>
 #include<iostream>
using namespace boost;
int main()
{
timer t;
//可度量的最大时间,单位小时
std::cout<<t.elapsed_max()/3600<<"h"<<std::endl;
//可度量的最小时间,单位秒
std::cout<<t.elapsed_min()<<"s"<<std::endl;
std::cout<<"now time:"<<t.elapsed()<<std::endl;
return 0;
}

输出结果

2.56205e+09h
1e-06s
now time:0.000194

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

progress_timer处理时间

 #include<boost/progress.hpp>
 #include<iostream>
int main()
{
    boost::progress_timer t;//开始计时
    std::cout << t.elapsed() << std::endl;//输出经历的时间
    std::cin.get();
    return 0;
}
progress_display显示处理进度 创建日期对象 处理日期的组件
 #include<boost/date_time/gregorian/gregorian.hpp>
 using namespace boost::gregorian;
处理时间的组件
#include<boost/date_time/posix_time/posix_time.hpp>
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

解决办法: 点击这里

内存管理
智能指针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<string>
 #include<iostream>
 #include<boost/smart_ptr.hpp>
int main()
{
    boost::scoped_ptr<std::string> 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<string>
 #include<iostream>
 #include<boost/smart_ptr.hpp>
 #include<assert.h>
int main()
{
    boost::shared_ptr<int> sp(new int(10));
    assert(sp.unique());//不需要加std。只需要包含assert.h头文件就可以。sp.unique来判断sp是否唯一。如果不唯一。立刻终止程序
    boost::shared_ptr<int> sp2 = sp;//进行拷贝构造函数
    *sp2 = 100;
    std::cout << *sp << std::endl;//拷贝构造函数修改,原始的指向也修改
    std::cin.get();
}
使用工厂函数进行批量赋值
  #include <iostream>
  #include <boost/make_shared.hpp>
  #include <string.h>
  #include <vector>
int main()
{
    auto sp = boost::make_shared<std::string>("hi david");
    auto spv = boost::make_shared<std::vector<int>>(10, 2);
    std::cin.get();
}
对一个容器当中的数据进行遍历,并最这些数据进行输出
  #include <iostream>
  #include <boost/make_shared.hpp>
  #include <boost/shared_ptr.hpp>
  #include <string.h>
  #include <vector>
//定义一个向量用来
typedef std::vector<boost::shared_ptr<int>> vs;
int main()
{
    //声明一个拥有10个元素的vector
    vs v(10);
    int i = 0;
    for (auto pos = v.begin(); pos != v.end();++pos)
    {
        //利用工厂函数进行赋值
        (*pos) = boost::make_shared<int>(++i);
            std::cout << *(*pos) << std::endl;
    }
}
工厂函数
  #include <iostream>
  #include <boost/make_shared.hpp>
  #include <boost/shared_ptr.hpp>
  #include <string.h>
  #include <vector>
//定义接口类
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<abstract> create()
{
    return boost::make_shared<impl>();
};
int main()
{
    //p的数据类型是一个指针。
    auto p = create();//工厂函数创建对象
    p->f();
    p->g();
    std::cin.get();
    return 0;
}

noncopy允许程序轻松实现一个禁止拷贝的类

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值