boost的安装
在线安装
Redhat/Centos :
sudo yum install boost-devel
Ubuntu :
sudo apt-get install libboost-dev
手动安装
大部分boost库的头文件主要由模板和内联函数实现,不需要编译成二进制文件。只需要解压即可。
- 下载boost : http://www.boost.org/
- 解压 :
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