C++之Boost使用

1. Get & Build & Install Boost

download boost from http://www.boost.org/

进入boost目录,使用命令:

./bootstrap.sh --prefix=path/to/installation

./b2 install

如此之后:

leave Boost binaries in the lib/ subdirectory of your installation prefix. You will also find a copy of the Boost headers in theinclude/ subdirectory of the installation prefix, so you can henceforth use that directory as an #include path in place of the Boost root directory.

 

2. Use Boost

1) Header-Only Libraries

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.

比如下面这个例子,使用的就是header-only的library.

 

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

 

编译:

 

c++ -I path/to/boost_1_47_0 example.cpp -o example

then:

 

echo 1 2 3 | ./example
 

 

 

2) Separately-Compiled Binary

 

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

 

 编译:

 

g++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test  \
-L /home/bin.jinb/usr/local/boost/lib/ -lboost_regex   

 

或者:

 

g++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test \
 /home/bin.jinb/usr/local/boost/lib/libboost_regex.a 
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值