BOOST基础

boost安装与使用

1. 简介

  • C++的一个准标准库

2. 安装

  • 在线安装
    Redhat/Centos
    sudo yum install boost-devel
    
    Ubuntu
    sudo apt-get install libboost-dev
    
  • 手动安装
    大部分boost库的头文件主要由模板和内联函数实现,不需要编译成二进制文件。只需要解压即可。
  1. 下载boost:http://www.boost.org/
  2. 解压tar -jxvf boost-版本号.tar.bz2

如果用到如下特性的时候需要安装。

Boost.Filesystem
Boost.IOStreams
Boost.ProgramOptions
Boost.Python
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.Thread
Boost.Wave

全安装方式

  1. 编译./bootstrap.sh --prefix=$HOME/usr
  2. 安装./b2 install

库的生成路径:$HOME/usr/lib,头文件的路径:$HOME/usr/include/boost

  1. 添加lib库自动搜索路径
    vim /etc/ld.so.conf,添加include /usr/local/lib
    4.运行ldconfig命令使之生效。

3. 使用

3.1 lamdba表达式

lambda库通过创建一个匿名的lambda表达式来代替实名的函数对象

  • HelloWorld.cpp
#include <iostream>  
#include <boost/lambda/lambda.hpp> 
using namespace boost::lambda; 
using namespace std; 
int main() {
    (cout << _1 << " " << _2)("Hello","World");
}
#include <iostream>  
#include <boost/lambda/lambda.hpp> 
using namespace boost::lambda; 
using namespace std;

int main(){  
    auto func = std::cout << _1 << " " << _2;
    func("Hello","World");
}
  • for_each.cpp
#include <iostream>  
#include <iterator>  
#include <algorithm>  
#include <vector>  
#include <boost/lambda/lambda.hpp>  
int main()  
{  
    using namespace boost::lambda;  
    using namespace std;  
    int arr[] = {1,2,3,4,5,6};
    for_each(arr,arr+6, cout << _1 << " " );
    cout << endl;

    vector<int> vec(arr,arr+6);
    for_each(vec.begin(),vec.end(), cout << _1 << " " );
    cout << endl;
}

如果需要使用_1_2的成员变量。需要使用绑定bind()

#include <iostream>  
#include <string>  
#include <map>  
#include <algorithm>  
#include "boost/lambda/lambda.hpp"  
#include "boost/lambda/bind.hpp"  

using namespace boost::lambda; 
using namespace std; 
int main() {  
    map<int,string> kv;
    kv[110] = "匪警";
    kv[119] = "火警";
    kv[120] = "急救中心";
    for_each(kv.begin(),kv.end(), cout << bind(&map<int,string>::value_type::first,_1) << '\t' <<  bind(&map<int,string>::value_type::second,_1) << '\n'); 
}

3.2 容器中存放任意类型值

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <boost/any.hpp>

int main(){
    using namespace std;
    using namespace boost;

    any a1 = 10;
    any a2 = 1.1f;
    any a3 = string("abc");
    any a4 = "abcefg";

    cout << any_cast<int>(a1) << endl;
    cout << any_cast<float>(a2) << endl;
    cout << any_cast<string&>(a3) << endl;
    cout << any_cast<const char*>(a4) << endl;

    vector<any> vec2 = {"abcd",123,3.14};
    cout << any_cast<const char*>(vec2[0]) << endl;
    cout << any_cast<int>(vec2[1]) << endl;
    cout << any_cast<double>(vec2[2]) << endl;

    vector<any> vec;
    vec.push_back(10);
    vec.push_back(12.4);
    vec.push_back(string("string"));
    char const* c_str = "const char*";
    vec.push_back(c_str);

    cout << any_cast<int>(vec[0]) << endl
            << any_cast<double>(vec[1]) << endl
            << any_cast<string&>(vec[2]) << endl
            << any_cast<char const*>(vec[3]) << endl;

    // copy(vec.begin(),vec.end(),ostream_iterator<any>(cout,","));
}

注意:

  1. any_cast<>中的类型不是原来的类型会抛出异常。
  2. any已经成为C++17的标准库组件。

3.3 数据转化

  • 字符串转数字
#include <iostream>
#include <boost/lexical_cast.hpp>
int main(){
    int i = boost::lexical_cast<int>("100");
    float f = boost::lexical_cast<float>("2.01");
    char chars[] = "1234";
    int n = boost::lexical_cast<int>(chars,strlen(chars));
    std::cout << i << " " << f << " " << n << std::endl;
    
    // f = boost::lexical_cast<int>("2.01"); // 类型不匹配抛异常
}

C++11可以使用stoi()stof()

  • 数字转字符串
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
int main(){
    std::string str1 = boost::lexical_cast<std::string>(100);
    std::string str2 = boost::lexical_cast<std::string>(2.01);
    std::cout << str1 << " " << str2 << std::endl;
}
  • 多态对象转换
#include <iostream>
#include <boost/cast.hpp>
using namespace std;
class Base{
public:
    void Func(){
        cout << "Base" << endl;
    }
    virtual ~Base(){}
};
class Derive : public Base{
public:
    void Func(){
        cout << "Derive" << endl;
    }
};
int main(){
    Base* b = new Derive;
    boost::polymorphic_cast<Derive*>(b)->Func();
}

3.4指针容器

特点:容器销毁自动释放指针

类型 说明
ptr_vector 指针向量
ptr_set 指针集合
ptr_array 指针数组
ptr_multimap 指针一对多映射
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/ptr_container/ptr_set.hpp>

int main(){
    ptr_vector<int> pvec;
    for(int i=0;i<10;++i){
        pvec.push_back(new int(i));
    }
    for(auto n:pvec){
        cout << n << " ";
    }

    boost::ptr_set<int> s;
    for(size_t i=0;i<10;i++){
        s.insert(new int(i));
    }
}

3.5 退出处理

变量离开(正常/异常)作用域,触发处理代码。

#include <iostream>
#include <boost/scope_exit.hpp>
using namespace std;
int main(){

    {
        int *a = new int(10);
        BOOST_SCOPE_EXIT(a){
            delete a;
        }BOOST_SCOPE_EXIT_END

        cout << *a << endl;
    }
}

不止用在new/delete,也用在fopen()/fclose()dlopen()/dlclose()等。与析构函数相似的作用。

3.6 遍历BOOST_FOREACH

#include <iostream>
#include <boost/foreach.hpp>
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5};
    BOOST_FOREACH(int a,arr) cout << a << endl;
}

3.7 函数绑定

  • 全局函数:boost::bind(函数名, 参数1,参数2,...)
  • 成员函数:boost::bind(&类名::方法名,类实例指针,参数1,参数2)
#include <iostream>
#include <algorithm>
#include <boost/bind.hpp>
using namespace std;
bool comp(int a,int b){
    return a<b;
}
int main(){
    int arr[] = {1,2,3,4,5};
    cout << count_if(arr,arr+5,boost::bind(comp,_1,3)) << endl;
    cout << count_if(arr,arr+5,boost::bind(comp,2,_1)) << endl;
}

bind支持最多九个参数。占位符被命名为 _1,_2, _3, _4, 直至 _9

3.8 不可复制类

#include <boost/nocopyable.hpp>

class Test:private boost::nocopyable{
    // ...  
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容