BOOST基础
1. 简介
- C++的一个准标准库
2. 安装
- 在线安装
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.Filesystem
Boost.IOStreams
Boost.ProgramOptions
Boost.Python
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.Thread
Boost.Wave
全安装方式
-
编译
./bootstrap.sh --prefix=$HOME/usr
. -
安装
./b2 install
库的生成路径:$HOME/usr/lib
,头文件的路径:$HOME/usr/include/boost
。 -
添加lib库自动搜索路径
vim /etc/ld.so.conf
,添加include /usr/local/lib
。 -
运行
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 << " "