什么是boost庫
簡單來說,Boost庫是為C++語言標准庫提供擴展的一些C++程序庫的總稱,由Boost社區組織開發、維護。Boost庫可以與C++標准庫完美共同工作,並且為其提供擴展功能。
C++11, C++14很多特性都是從boost庫來的,很多 boost 庫采用 STL 的風格,需要 STL 的基礎。建議先用好 STL 再去看 boost。
boost 更准確的說,並不是一個庫,而是一個庫集合。不用每個都去看。應該先快速翻閱一些簡介,只需要了解一下有什么庫,每個庫大概是做什么東西的。之后碰到問題再細看具體的庫用法。
更詳細的介紹可以看以下鏈接:
Boost庫 - 百度百科
學習C++有沒有必要學習boost庫? - 知乎
boost庫的下載和安裝
下載
網上有的教程揮手一指就讓到boost官網下載,本來我想着從官網下載挺好,然而官網點擊下載之后,居然又給我跳到什么JFrog網站,又要安裝JFrog相關的配套環境才能從JFrog上下載我需要的boost庫,這啥呀想想就頭大,這么能繞,不干。
我比較推薦以下這兩個鏈接,直接下載壓縮包,之后再按照相關教程配置就可以了,方便多了。
我用的是第二個。
安裝
我下載的是 boost_1_77_0 。
解壓文件,然后打開到boost庫的根目錄下:
雙擊bootstrap.bat文件,生成b2.exe,然后打開b2.exe,在cmd中輸入以下命令:
bjam --toolset=msvc --build-type=complete stage ,然后回車
等待程序編譯完成,大約要十幾分鍾到兩個小時左右,會在boost根目錄下生成bin.v2和stage兩個文件夾,其中bin.v2下是生成的中間文件,大小在2.2G左右,可以直接刪除。stage下才是生成的dll和lib文件。
VS項目屬性配置(VS2019)
項目->屬性
在彈出的屬性對話框中:
配置屬性->VC++目錄:"包含目錄": boost的根目錄,例: D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0
"庫目錄": stage下的鏈接庫目錄,例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
配置屬性->鏈接器->常規:"附加庫目錄":同上面的"庫目錄",例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
至此環境就配置好了,下面測試一下:
點擊查看代碼
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <libs/date_time/src/gregorian/greg_names.hpp>
#include <libs/date_time/src/gregorian/greg_month.cpp>
#include <libs/date_time/src/gregorian/gregorian_types.cpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
int main()
{
boost::timer t;
boost::progress_display pd(100);
for (int i = 0; i < 100; ++i) //進度條
{
++pd;
}
boost::gregorian::date dt(2009, 12, 8); //date_time 庫
assert(dt.year() == 2009);
assert(dt.day() == 8);
boost::gregorian::date::ymd_type ymd = dt.year_month_day();
std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is "
<<dt.day_of_year() <<" days of this year"<< std::endl;
std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //轉換為其他格式
std::cout << boost::gregorian::to_iso_string(dt) << std::endl;
std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl;
//對數組排序操作
std::vector<int> test_vc(100);
std::vector<int>::iterator beg_it = test_vc.begin();
std::vector<int>::iterator end_it = test_vc.end();
std::srand(std::time(NULL));
std::for_each(beg_it, end_it, [](int& n){n = rand(); });
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl << std::endl;
std::sort(beg_it, end_it, std::greater<int>());
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl<<std::endl;
boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6));
std::cout << t.elapsed() << "s" << std::endl; //程序運行時間
system("pause");
return 0;
}
注:在原帖中的代碼出現了小問題,在我的vs版本編譯時報錯:C2084 函數“const char *boost::date_time::nth_as_str(int)”已有主體 ,錯誤來源於date_generators.cpp文件。
解決方法:直接刪去如下語句即可
#include <libs/date_time/src/gregorian/date_generators.cpp>
猜測是此boost庫中的內容已經被添加到新的C++標准庫中,所以此文件中函數定義與C++原本的標准庫中函數定義沖突,所以直接刪去后程序可以正常運行。
程序正確運行:
參考文章:windows下boost庫的基本使用方法 ,有刪改。