Boost的安装与使用

https://blog.csdn.net/weixin_39398433/article/details/118668269

文章目录

boost的安装

在线安装
Redhat/Centos :

  sudo yum install boost-devel

Ubuntu :

  sudo apt-get install libboost-dev

手动安装
大部分boost库的头文件主要由模板和内联函数实现,不需要编译成二进制文件。只需要解压即可。

  1. 下载boost : Boost C++ Libraries
  2. 解压 : tar -jxvf boost-版本号.tar.bz2

boost的使用

1. lamdba表达式

打印数字和字符

#include <iostream>
#include <boost/lambda/lambda.hpp>
using namespace std;       // 标准库
using namespace boost::lambda;     // boost lambda库

int main(){
        // (cout << _1 << " " << _2)(3,4);
        auto func = (cout << _1 << " " << _2);
        func(1,"abcd");
}

结果为:

1 abcd

2. 容器中存放任意类型值

可以把任意类型的数据放在any中,用的时候再any_cast出类型

#include <iostream>
#include <boost/any.hpp>
using namespace std;       // 标准库
using namespace boost;     // boost库

int main(){
        any n = 10;
        any f = 1.23f;
        any s = string("abcd");

        cout << any_cast<int>(n) << endl;
        cout << any_cast<float>(f) << endl;
        cout << any_cast<string&>(s) << endl;
}

结果为:

10
1.23
abcd

3. 数据类型转换

字符串转整型或浮点型

#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;       // 标准库
using namespace boost;     // boost库

int main(){
        cout << lexical_cast<int>("1000") << endl;
        cout << lexical_cast<float>("1.23000") << endl;

        cout << stoi("1000") << endl;     // C++11标准库
        cout << stof("1.23") << endl;
}

结果为:

1000
1.23
1000
1.23

==字符串转换为类的类型 ==

#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;    
using namespace boost;    

class People{
        string name;
        long telephone;
        string address;
public:
        friend ostream& operator << (ostream& os,const People& p){
                return os << p.name << "\t" << p.telephone << "\t" << p.address;
        }
        friend istream& operator >> (istream& is,People& p){
                return is >> p.name >> p.telephone >> p.address;
        }
};

int main(){
        People p;   // = lexical_cast<People>("zhangsan 12345 abcde");
        istringstream iss("zhangsan 12345 abcde");
        iss >> p;
        cout << p << endl;
}

结果为:

zhangsan	12345	abcde

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));
    }
}

5. 退出处理

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

#include <iostream>
#include <boost/scope_exit.hpp>
using namespace std;  
using namespace boost; 

class Test{
public:
        Test(){cout << __func__ << endl;}
        ~Test(){cout << __func__ << endl;}
};

int main(){
        Test t;
        Test* pt = new Test;

        BOOST_SCOPE_EXIT(pt){
                delete pt;           // 离开作用域实行
        }BOOST_SCOPE_EXIT_END

        cout << "abcd" << endl;
}

结果为:

Test
Test
abcd
~Test
~Test

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

6. 遍历

#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;
}

7. 函数绑定

全局函数:

 boost::bind(函数名, 参数1,参数2,...)

把参数中的一个函数参数固定,其他的再赋值

#include <iostream>
#include <boost/bind.hpp>
using namespace std;     
using namespace boost;    

int add(int a,int b,int c){
        return a+b+c;
}

int main(){
        auto add2 = bind(add,_1,_2,0);
        cout << add2(10,12) << endl;
}

结果为:

22

成员函数:

 boost::bind(&类名::方法名,类实例指针,参数1,参数2)
#include <iostream>
#include <boost/bind.hpp>
using namespace std;  
using namespace boost;     

class Test{
public:
        Test(){cout << __func__ << endl;}
        ~Test(){cout << __func__ << endl;}
        void Func(int n)const{
                cout << n << endl;
        }
};

int main(){
        Test t;
        t.Func(1000);
        auto funct = bind(&Test::Func,&t,_1);
        funct(2000);
}

结果为:

Test
1000
2000
~Test

8. 不可复制类

用boost库可以让类不可复制

#include <boost/nocopyable.hpp>

class Test:private boost::nocopyable{
    // ...  
};

在C++中 ,哪些方法可以禁止复制和重载?

  1. 设为私有
  2. 加=delete
class Test{
public:
        Test(){cout << __func__ << endl;}
        ~Test(){cout << __func__ << endl;}
        void Func(int n)const{
                cout << n << endl;
        }

        // C++11
        // Test(const Test&) = delete,
        // Test& operator = (const Test&) = delete;
private:
        Test(const Test&);
        Test& operator = (const Test&);
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值