boost.Multi_Index庫的使用,boost.multi_index庫

來源:互聯網
上載者:User

boost.Multi_Index庫的使用,boost.multi_index庫

#include "stdafx.h"#include <list>#include <string>#include <boost/multi_index_container.hpp>#include <boost/multi_index/ordered_index.hpp>#include <boost/multi_index/identity.hpp>#include <boost/multi_index/member.hpp>#include <boost/lambda/lambda.hpp>/*標題:boost.Multi_Index庫的使用功能:類似std::map的容器,但是可以用多個關鍵詞索引資料描述:boost::multi_index::multi_index_container為容器內的資料建立多個視圖     不同的索引欄位(Key)對應不同的視圖 boost::multi_index::multi_index_container經常可以採用不同的表達方式實現 同樣的功能。 這裡只列舉一種能滿足我們添加、刪除、修改、檢索記錄的需求範例程式碼,有興趣的話 可以通過參考資料中的官網地址得到如何使用其它方式達到這裡功能需求的知識。環境:Windows 8.1 64bit(英文版)  Visual Studio 2013 Professional with SP1  boost 1.55注意:編寫和template相關的代碼一旦出錯不會給出正確的錯誤位置      所以建議每完成一個函數,就寫一個測試這個函數的函數。最後更新日期:2014-5-10應用範圍:定製自己的記憶體資料庫參考資料:http://www.boost.org/doc/libs/1_55_0/libs/multi_index/doc/index.html*/#pragma region 定義 employee //使用名字空間是為了防止資料類型名稱衝突namespace kagula{namespace datatype{//為了後面的代碼能通過id和name名字取視圖,定義了這兩個結構struct id{};struct name{};//employee是我們範例程式碼中表的結構struct employee{int          id;std::wstring name;employee(int id, const std::wstring& name) :id(id), name(name){}//重載<運算子函數讓後面名為id的視圖按照id欄位的升序排列bool operator<(const employee& e)const{ return id<e.id; }//重載<=運算子函數是為了得到指定id欄位值範圍的記錄bool operator<=(const employee& e)const{ return id<=e.id; }//重載<<運算子函數是為了我們方便列印記錄的內容friend std::wostream& operator<<(std::wostream& os, const employee& dt);};std::wostream& operator<<(std::wostream& os, const employee& dt){os << L"[" << dt.id << L',' << dt.name.c_str() << L"]";return os;}//後面的代碼,更新指定記錄name欄位的值要用到change_name類//通過這個樣本你可以知道如何修改符合搜尋條件的,指定記錄的任意欄位值struct change_name{change_name(const std::wstring& new_name) :new_name(new_name){}void operator()(employee& e){e.name = new_name;}private:std::wstring new_name;};// 定義基於id和name關鍵詞的多值索引集合類型// 這個容器有id和name兩個視圖typedef boost::multi_index::multi_index_container<employee,boost::multi_index::indexed_by<//第一個視圖,使用employee執行個體作為key,所以採用了employee::operator<運算子函數排序,因此基於id欄位//boost::multi_index::ordered_unique中的unique指定key只能唯一,這樣就無法插入id欄位值相同的記錄了//boost::multi_index::ordered_unique中的ordered指定第一個視圖,按照key的升序排列,哪怕你插入的資料順序是亂的//在這裡你可以定義多個boost::multi_index::ordered_unique類型的視圖//boost::multi_index::identity把對象作為索引的key//boost::multi_index::tag<id>是為了以後能通過id名取第一個視圖//你可以去掉boost::multi_index::tag<id>,如果你不需要根據名字id來取視圖的話boost::multi_index::ordered_unique<boost::multi_index::tag<id>, boost::multi_index::identity<employee> >,//第二個視圖,它的key是employee對象的name欄位,所以它的key類型是std::wstring//所以使用了std::wstring::operator<運算子函數排序//boost::multi_index::ordered_non_unique中的non_unique指定這個key允許有重複.//boost::multi_index::ordered_non_unique中的ordered指定第二視圖,以這個key升序排列.//如果你不需要視圖按照key的順序排列,可以用hashed代替ordered,例如//boost::multi_index::ordered_non_unique 被代替為 boost::multi_index::hashed_non_unique//就可以避免對插入的資料排序,需要包含<boost/multi_index/hashed_index.hpp>標頭檔//boost::multi_index::member把對象的成員作為索引的key//boost::multi_index::tag<name>是為了以後的代碼能通過name名取第二個視圖,當視圖很多的時候,這個功能將會非常有用boost::multi_index::ordered_non_unique<boost::multi_index::tag<name>, boost::multi_index::member<employee, std::wstring, &employee::name> >//這裡視圖的定義採用了下面的文法形式//(ordered_unique | ordered_non_unique)<[(key extractor)[, (comparison predicate)]]>//作為key的物件類型必須實現<運算子函數,除非你指定了comparison predicate.>> employee_set;}}#pragma endregionusing namespace kagula::datatype;void print_out_by_id(const employee_set& es){//得到id視圖(它是基於id欄位的)const employee_set::index<id>::type& id_index = es.get<id>();//下面注釋掉的代碼,是基於序號索引檢視表,0代表第一個視圖,1代表第二個視圖,依次類推//const employee_set::nth_index<0>::type& id_index = es.get<0>();//採用同std::set容器一樣的方式列印元素std::copy(id_index.begin(), id_index.end(),std::ostream_iterator<employee, wchar_t>(std::wcout));std::wcout << std::endl << std::endl;}void print_out_by_name(const employee_set& es){//得到name視圖(它是基於name欄位的)const employee_set::index<name>::type& name_index = es.get<name>();//下面注釋掉的代碼,是基於序號索引檢視表,代表第二個視圖,依次類推//const employee_set::nth_index<1>::type& name_index = es.get<1>();//採用同std::set容器一樣的方式列印元素std::copy(name_index.begin(), name_index.end(),std::ostream_iterator<employee,wchar_t>(std::wcout));std::wcout << std::endl << std::endl;}//建立測試資料void CreateSample(employee_set& table){table.insert(employee(0, L"Z"));table.insert(employee(1, L"Z"));table.insert(employee(2, L"X"));//Error:下面這條記錄無法成功插入,因為在建立第一張視圖時指定了id必須唯一//      所以執行插入編號為2的記錄會沒有效果, 即table中id為2的記錄它還是原來的值//      但是代碼也不會拋出Excetion。//table.insert(employee(2, L"新插入的id為2的記錄"));//插入name欄位的值為"Z"成功//雖然name欄位的值已經存在,但建立第二視圖的時候指定了name欄位的值可以有重複//所以下面的添加記錄功能可以正常運行table.insert(employee(3, L"Z"));table.insert(employee(100, L"Judy Smith"));table.insert(employee(101, L"Judy Smith"));table.insert(employee(200, L"Anna Jones"));table.insert(employee(201, L"Anna Jones"));}void select_stat(employee_set& table){std::wcout << L"第一個視圖按照id欄位的升序排列" << std::endl; print_out_by_id(table);{std::wcout << L"第一個視圖按照id欄位的降序列印" << std::endl;const employee_set::index<id>::type&  viewId = table.get<id>();std::copy(viewId.rbegin(), viewId.rend(),std::ostream_iterator<employee, wchar_t>(std::wcout));std::wcout << std::endl << std::endl;}std::wcout << L"第二個視圖按照name欄位的升序排列" << std::endl;print_out_by_name(table);{std::wcout << L"檢索出id==2的記錄" << std::endl;employee cond(2, L"");employee_set::index<id>::type::iterator iter = table.find(cond);std::wcout << *iter << std::endl << std::endl;}{std::wcout << L"通過boost::lambda檢索出指定id範圍的記錄集合" << std::endl;;std::pair<employee_set::iterator, employee_set::iterator> p;p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1 <= employee(200, L"")); // 100<= x <=200//下面被注釋掉的代碼,示範了如何取指定範圍的記錄//p = table.range(employee(100, L"")<boost::lambda::_1, boost::lambda::_1<employee(200, L""));   // 100<  x < 200//p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1<employee(200, L""));  // 100<= x < 200//p = table.range(employee(100, L"") <= boost::lambda::_1, boost::multi_index::unbounded); // 100 <= x//p = table.range(boost::multi_index::unbounded, boost::lambda::_1<employee(200, L""));  //   x <  200//p = table.range(boost::multi_index::unbounded, boost::multi_index::unbounded); // equiv. to std::make_pair(s.begin(),s.end())for (employee_set::iterator it = p.first; it != p.second; it++){std::wcout << *it << L" , ";}std::wcout << std::endl << std::endl;}{std::wcout << L"檢索出name==Judy Smith的第一條記錄" << std::endl;const employee_set::index<name>::type& viewName = table.get<name>();employee_set::index<name>::type::iterator it = viewName.find(L"Judy Smith");std::wcout << *it << std::endl << std::endl;std::wcout << L"統計name==Judy Smith的記錄有多少" << std::endl;unsigned int count = viewName.count(L"Judy Smith");std::wcout << count << std::endl << std::endl;}{std::wcout << L"檢索出name==z的記錄集合" << std::endl;;const employee_set::index<name>::type& viewName = table.get<name>();std::pair<employee_set::index<name>::type::iterator, employee_set::index<name>::type::iterator> p;p = viewName.equal_range(L"Z"); for (employee_set::index<name>::type::iterator it = p.first; it != p.second; it++){std::wcout << *it << L" , ";}std::wcout << std::endl << std::endl;}}void delete_stat(employee_set& table){//從表中刪除id值為2的的記錄,name欄位可以為任意值{std::wcout << L"刪除id==2的記錄" << std::endl;employee cond(2, L"");int nCount = table.erase(cond); //nCount為1,因為我們的id值是唯一的。//如果id欄位不是唯一的,我們可以通過判斷nCount,刪除所有合格記錄std::wcout << L"有" << nCount << L"條記錄被刪除。" << std::endl << std::endl;}//從表中刪除name欄位值為Z的全部記錄{std::wcout << L"刪除name==Z的所有記錄" << std::endl;employee_set::index<name>::type& viewName = table.get<name>();while ( table.erase(*viewName.find(L"Z")) > 0 );print_out_by_id(table);}//重建立立樣本table.clear();CreateSample(table);//刪除id在指定範圍的記錄std::wcout << L"刪除100<= id <=200的所有記錄" << std::endl;std::pair<employee_set::iterator, employee_set::iterator> p;p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1 <= employee(200, L"")); // 100<= x <=200table.erase(p.first, p.second);print_out_by_id(table);}void update_stat(employee_set& table){std::wcout << L"替換名為Anna Jones的記錄為Anna Smith,但是只修改合格第一條記錄!" << std::endl;typedef employee_set::index<name>::type employee_set_by_name;//因為要修改iter所指向的資料所以下面的語句不能使用const修飾employee_set_by_name& name_index = table.get<name>();employee_set_by_name::iterator it = name_index.find(L"Anna Jones");//修改key的值,key必須可以讀寫name_index.modify_key(it, boost::lambda::_1 = L"Anna Smith");print_out_by_id(table);std::wcout << L"把名為Anna Smith的第一條記錄修改為Anna Jones" << std::endl;//下面這種方法可以修改對象某個屬性的值,但是需要定義change_name類it = name_index.find(L"Anna Smith");name_index.modify(it, change_name(L"Anna Jones"));print_out_by_id(table);{std::wcout << L"修改所有name==Z的記錄為kagula" << std::endl;employee_set_by_name& viewName = table.get<name>();employee_set_by_name::iterator it;while ( (it = viewName.find(L"Z")) != viewName.end() ){viewName.modify(it, change_name(L"kagula"));}print_out_by_id(table);//Warnning://不要在生產環境中,使用下面的代碼來修改所有合格記錄//因為如果沒有上邊的程式碼片段,下面的代碼只會修改第一條合格記錄//後,提早退出迴圈。std::wcout << L"修改所有name==kagula的記錄為Z" << std::endl;viewName = table.get<name>();std::pair<employee_set_by_name::iterator, employee_set_by_name::iterator> p;p = viewName.equal_range(L"kagula");for (employee_set_by_name::iterator it = p.first; it != p.second; it++){viewName.modify(it, change_name(L"Z"));}print_out_by_id(table);}//因為修改記錄的值是通過it對象,所以可以使用select_stat介紹的知識修改指定範圍的記錄}int _tmain(int argc, _TCHAR* argv[]){//使std::wcout能夠列印中文std::wcout.imbue(std::locale(std::locale("chs")));//建立樣本//等價SQL,Add語句的相關功能employee_set table;CreateSample(table);//這裡測試等價SQL,Select語句的相關功能select_stat(table);//這裡測試等價SQL,Update語句的相關功能update_stat(table);//這裡測試等價SQL,Delete語句的相關功能delete_stat(table);//暫停,輸入任意鍵繼續system("pause");return 0;}


BOOST庫使用問題

你把編譯後的所有 .lib 檔案複製到 vs2010 下
如vs在c盤
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ 下有 boost 檔案夾,裡面有用的標頭檔

C:\Program Files\Microsoft Visual Studio 10.0\VC\lib 下有 你的 .lib 檔案

不然vc 找不到 include 和 link 路徑
我當初就是這樣搞的……
 
boost庫需要編譯?是否是可以直接使用的那種

必須編譯。編譯時間可能是15~30分鐘,等待即可,否則無法使用。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.