1、簡介:
Boost是一個同行評審、提供源代碼、可移植的C++庫。它可以與C++標准庫完美地共同工作,並為其提供擴展功能。目前,有一部分Boost庫已經被包含在C++標准委員會的程序庫技術報告TR1中,並在即將到來的C++標准修訂版本中加入。
2、下載、安裝:
從http://www.boost.org/users/download/下載合適版本的壓縮包(如boost_1_57_0.tar.gz) -> 解壓為/usr/local/boost_1_57_0,並加一個軟鏈boost指向它。
大部分Boost庫的使用只需包含相應頭文件即可,少數(如coroutine、python、regex和thread)需要鏈接相應的二進制庫(library binary)。這些庫需要自己編譯和安裝:
cd /usr/local/boost
# 參考./bootstrap.sh --help
# --show-libraries顯示那些需要編譯(build)和安裝步驟的庫的集合 # --with-libraries=list:list或取值"all",或指定要編譯的庫的集合,用逗號分割 ./bootstrap.sh --show-libraries --with-libraries=regex
# 把頭文件和已編譯的庫文件安裝到指定路徑(/usr/local/include/boost/和/usr/local/lib/等) ./b2 install
3、示例:
// example.cpp
#include <boost/regex.hpp> // ... 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)) { // matches[2]對應pat第2個括號內正則表達式匹配到的字符串,依此類推 std::cout << matches[2] << std::endl; } } }
編譯:g++ example.cpp -I /usr/local/boost /usr/local/lib/libboost_regex.a。運行:
$ ./a.out Subject: Re: Re: Re: How To Build Boost? How To Build Boost?
參考資料:
http://www.boost.org/
不斷學習中。。。
