boost.Multi_Index库的使用

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2.   
  3. #include <list>  
  4. #include <string>  
  5.   
  6. #include <boost/multi_index_container.hpp>  
  7. #include <boost/multi_index/ordered_index.hpp>  
  8. #include <boost/multi_index/identity.hpp>  
  9. #include <boost/multi_index/member.hpp>  
  10.   
  11. #include <boost/lambda/lambda.hpp>  
  12. /* 
  13. 标题:boost.Multi_Index库的使用 
  14. 功能:类似std::map的容器,但是可以用多个关键词索引数据 
  15. 描述:boost::multi_index::multi_index_container为容器内的数据建立多个视图 
  16.      不同的索引字段(Key)对应不同的视图 
  17.      boost::multi_index::multi_index_container经常可以采用不同的表达方式实现 
  18.      同样的功能。 
  19.      这里只列举一种能满足我们添加、删除、修改、检索记录的需求示例代码,有兴趣的话 
  20.      可以通过参考资料中的官网地址得到如何使用其它方式达到这里功能需求的知识。 
  21. 环境:Windows 8.1 64bit(英文版) 
  22.       Visual Studio 2013 Professional with SP1 
  23.       boost 1.55 
  24. 注意:编写和template相关的代码一旦出错不会给出正确的错误位置 
  25.       所以建议每完成一个函数,就写一个测试这个函数的函数。 
  26. 最后更新日期:2014-5-10 
  27. 应用范围:定制自己的内存数据库 
  28. 参考资料:http://www.boost.org/doc/libs/1_55_0/libs/multi_index/doc/index.html 
  29. */  
  30.   
  31. #pragma region 定义 employee   
  32. //使用名字空间是为了防止数据类型名称冲突  
  33. namespace kagula  
  34. {  
  35.     namespace datatype  
  36.     {  
  37.         //为了后面的代码能通过id和name名字取视图,定义了这两个结构  
  38.         struct id{};  
  39.         struct name{};  
  40.   
  41.         //employee是我们示例代码中表的结构  
  42.         struct employee  
  43.         {  
  44.             int          id;  
  45.             std::wstring name;  
  46.   
  47.             employee(int id, const std::wstring& name) :id(id), name(name){}  
  48.   
  49.             //重载<运算符函数让后面名为id的视图按照id字段的升序排列  
  50.             bool operator<(const employee& e)constreturn id<e.id; }  
  51.             //重载<=运算符函数是为了得到指定id字段值范围的记录  
  52.             bool operator<=(const employee& e)constreturn id<=e.id; }  
  53.             //重载<<运算符函数是为了我们方便打印记录的内容  
  54.             friend std::wostream& operator<<(std::wostream& os, const employee& dt);  
  55.         };  
  56.         std::wostream& operator<<(std::wostream& os, const employee& dt)  
  57.         {  
  58.             os << L"[" << dt.id << L',' << dt.name.c_str() << L"]";  
  59.             return os;  
  60.         }  
  61.         //后面的代码,更新指定记录name字段的值要用到change_name类  
  62.         //通过这个示例你可以知道如何修改符合搜索条件的,指定记录的任意字段值  
  63.         struct change_name  
  64.         {  
  65.             change_name(const std::wstring& new_name) :new_name(new_name){}  
  66.   
  67.             void operator()(employee& e)  
  68.             {  
  69.                 e.name = new_name;  
  70.             }  
  71.   
  72.         private:  
  73.             std::wstring new_name;  
  74.         };  
  75.   
  76.         // 定义基于id和name关键词的多值索引集合类型  
  77.         // 这个容器有id和name两个视图  
  78.         typedef boost::multi_index::multi_index_container<  
  79.             employee,  
  80.   
  81.             boost::multi_index::indexed_by<  
  82.             //第一个视图,使用employee实例作为key,所以采用了employee::operator<运算符函数排序,因此基于id字段  
  83.             //boost::multi_index::ordered_unique中的unique指定key只能唯一,这样就无法插入id字段值相同的记录了  
  84.             //boost::multi_index::ordered_unique中的ordered指定第一个视图,按照key的升序排列,哪怕你插入的数据顺序是乱的  
  85.             //在这里你可以定义多个boost::multi_index::ordered_unique类型的视图  
  86.             //boost::multi_index::identity把对象作为索引的key  
  87.             //boost::multi_index::tag<id>是为了以后能通过id名取第一个视图  
  88.             //你可以去掉boost::multi_index::tag<id>,如果你不需要根据名字id来取视图的话  
  89.             boost::multi_index::ordered_unique<boost::multi_index::tag<id>, boost::multi_index::identity<employee> >,  
  90.             //第二个视图,它的key是employee对象的name字段,所以它的key类型是std::wstring  
  91.             //所以使用了std::wstring::operator<运算符函数排序  
  92.             //boost::multi_index::ordered_non_unique中的non_unique指定这个key允许有重复.  
  93.             //boost::multi_index::ordered_non_unique中的ordered指定第二视图,以这个key升序排列  .  
  94.             //如果你不需要视图按照key的顺序排列,可以用hashed代替ordered,例如  
  95.             //boost::multi_index::ordered_non_unique 被代替为 boost::multi_index::hashed_non_unique  
  96.             //就可以避免对插入的数据排序,需要包含<boost/multi_index/hashed_index.hpp>头文件  
  97.             //boost::multi_index::member把对象的成员作为索引的key  
  98.             //boost::multi_index::tag<name>是为了以后的代码能通过name名取第二个视图,当视图很多的时候,这个功能将会非常有用  
  99.             boost::multi_index::ordered_non_unique<boost::multi_index::tag<name>, boost::multi_index::member<employee, std::wstring, &employee::name> >  
  100.             //这里视图的定义采用了下面的语法形式  
  101.             //(ordered_unique | ordered_non_unique) <[(key extractor)[, (comparison predicate)]]>  
  102.             //作为key的对象类型必须实现<运算符函数,除非你指定了comparison predicate.  
  103.             >  
  104.   
  105.         > employee_set;  
  106.     }  
  107. }  
  108. #pragma endregion  
  109.   
  110. using namespace kagula::datatype;  
  111.   
  112.   
  113. void print_out_by_id(const employee_set& es)  
  114. {  
  115.     //得到id视图(它是基于id字段的)  
  116.     const employee_set::index<id>::type& id_index = es.get<id>();  
  117.       
  118.     //下面注释掉的代码,是基于序号索引视图,0代表第一个视图,1代表第二个视图,依次类推  
  119.     //const employee_set::nth_index<0>::type& id_index = es.get<0>();  
  120.       
  121.     //采用同std::set容器一样的方式打印元素  
  122.     std::copy(  
  123.         id_index.begin(), id_index.end(),  
  124.         std::ostream_iterator<employee, wchar_t>(std::wcout));  
  125.     std::wcout << std::endl << std::endl;  
  126. }  
  127.   
  128. void print_out_by_name(const employee_set& es)  
  129. {  
  130.     //得到name视图(它是基于name字段的)  
  131.     const employee_set::index<name>::type& name_index = es.get<name>();  
  132.   
  133.     //下面注释掉的代码,是基于序号索引视图,代表第二个视图,依次类推  
  134.     //const employee_set::nth_index<1>::type& name_index = es.get<1>();  
  135.   
  136.     //采用同std::set容器一样的方式打印元素  
  137.     std::copy(  
  138.         name_index.begin(), name_index.end(),  
  139.         std::ostream_iterator<employee,wchar_t>(std::wcout));  
  140.     std::wcout << std::endl << std::endl;  
  141. }  
  142.   
  143. //建立测试数据  
  144. void CreateSample(employee_set& table)  
  145. {  
  146.     table.insert(employee(0, L"Z"));  
  147.     table.insert(employee(1, L"Z"));  
  148.     table.insert(employee(2, L"X"));  
  149.     //Error:下面这条记录无法成功插入,因为在建立第一张视图时指定了id必须唯一  
  150.     //      所以执行插入编号为2的记录会没有效果, 即table中id为2的记录它还是原来的值  
  151.     //      但是代码也不会抛出Excetion。  
  152.     //table.insert(employee(2, L"新插入的id为2的记录"));  
  153.   
  154.     //插入name字段的值为"Z"成功  
  155.     //虽然name字段的值已经存在,但建立第二视图的时候指定了name字段的值可以有重复  
  156.     //所以下面的添加记录功能可以正常运行  
  157.     table.insert(employee(3, L"Z"));  
  158.   
  159.   
  160.     table.insert(employee(100, L"Judy Smith"));  
  161.     table.insert(employee(101, L"Judy Smith"));  
  162.   
  163.     table.insert(employee(200, L"Anna Jones"));  
  164.     table.insert(employee(201, L"Anna Jones"));  
  165. }  
  166.   
  167.   
  168. void select_stat(employee_set& table)  
  169. {  
  170.     std::wcout << L"第一个视图按照id字段的升序排列" << std::endl;   
  171.     print_out_by_id(table);  
  172.   
  173.     {  
  174.         std::wcout << L"第一个视图按照id字段的降序打印" << std::endl;  
  175.         const employee_set::index<id>::type&  viewId = table.get<id>();  
  176.         std::copy(viewId.rbegin(), viewId.rend(),  
  177.             std::ostream_iterator<employee, wchar_t>(std::wcout));  
  178.         std::wcout << std::endl << std::endl;  
  179.     }  
  180.   
  181.     std::wcout << L"第二个视图按照name字段的升序排列" << std::endl;  
  182.     print_out_by_name(table);  
  183.   
  184.     {  
  185.         std::wcout << L"检索出id==2的记录" << std::endl;  
  186.         employee cond(2, L"");  
  187.         employee_set::index<id>::type::iterator iter = table.find(cond);  
  188.         std::wcout << *iter << std::endl << std::endl;  
  189.     }  
  190.   
  191.     {  
  192.         std::wcout << L"通过boost::lambda检索出指定id范围的记录集合" << std::endl;;  
  193.         std::pair<employee_set::iterator, employee_set::iterator> p;  
  194.         p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1 <= employee(200, L"")); // 100<= x <=200  
  195.         //下面被注释掉的代码,演示了如何取指定范围的记录  
  196.         //p = table.range(employee(100, L"")<boost::lambda::_1, boost::lambda::_1<employee(200, L""));   // 100<  x < 200  
  197.         //p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1<employee(200, L""));  // 100<= x < 200  
  198.         //p = table.range(employee(100, L"") <= boost::lambda::_1, boost::multi_index::unbounded); // 100 <= x  
  199.         //p = table.range(boost::multi_index::unbounded, boost::lambda::_1<employee(200, L""));  //   x <  200  
  200.         //p = table.range(boost::multi_index::unbounded, boost::multi_index::unbounded); // equiv. to std::make_pair(s.begin(),s.end())       
  201.         for (employee_set::iterator it = p.first; it != p.second; it++)  
  202.         {  
  203.             std::wcout << *it << L" , ";  
  204.         }  
  205.         std::wcout << std::endl << std::endl;  
  206.     }  
  207.   
  208.     {  
  209.         std::wcout << L"检索出name==Judy Smith的第一条记录" << std::endl;  
  210.         const employee_set::index<name>::type& viewName = table.get<name>();  
  211.         employee_set::index<name>::type::iterator it = viewName.find(L"Judy Smith");  
  212.         std::wcout << *it << std::endl << std::endl;  
  213.   
  214.         std::wcout << L"统计name==Judy Smith的记录有多少" << std::endl;  
  215.         unsigned int count = viewName.count(L"Judy Smith");  
  216.         std::wcout << count << std::endl << std::endl;  
  217.     }  
  218.   
  219.     {  
  220.         std::wcout << L"检索出name==z的记录集合" << std::endl;;  
  221.         const employee_set::index<name>::type& viewName = table.get<name>();  
  222.         std::pair<employee_set::index<name>::type::iterator, employee_set::index<name>::type::iterator> p;  
  223.   
  224.         p = viewName.equal_range(L"Z");   
  225.   
  226.         for (employee_set::index<name>::type::iterator it = p.first; it != p.second; it++)  
  227.         {  
  228.             std::wcout << *it << L" , ";  
  229.         }  
  230.         std::wcout << std::endl << std::endl;  
  231.     }  
  232.   
  233. }  
  234.   
  235. void delete_stat(employee_set& table)  
  236. {  
  237.     //从表中删除id值为2的的记录,name字段可以为任意值  
  238.     {  
  239.         std::wcout << L"删除id==2的记录" << std::endl;  
  240.         employee cond(2, L"");  
  241.         int nCount = table.erase(cond);   
  242.         //nCount为1,因为我们的id值是唯一的。  
  243.         //如果id字段不是唯一的,我们可以通过判断nCount,删除所有符合条件的记录  
  244.         std::wcout << L"有" << nCount << L"条记录被删除。" << std::endl << std::endl;  
  245.     }  
  246.   
  247.     //从表中删除name字段值为Z的全部记录  
  248.     {  
  249.         std::wcout << L"删除name==Z的所有记录" << std::endl;  
  250.         employee_set::index<name>::type& viewName = table.get<name>();  
  251.         while ( table.erase(*viewName.find(L"Z")) > 0 );  
  252.         print_out_by_id(table);  
  253.     }  
  254.   
  255.     //重新建立样本  
  256.     table.clear();  
  257.     CreateSample(table);  
  258.   
  259.     //删除id在指定范围的记录  
  260.     std::wcout << L"删除100<= id <=200的所有记录" << std::endl;  
  261.     std::pair<employee_set::iterator, employee_set::iterator> p;  
  262.     p = table.range(employee(100, L"") <= boost::lambda::_1, boost::lambda::_1 <= employee(200, L"")); // 100<= x <=200  
  263.     table.erase(p.first, p.second);  
  264.     print_out_by_id(table);  
  265. }  
  266.   
  267. void update_stat(employee_set& table)  
  268. {  
  269.     std::wcout << L"替换名为Anna Jones的记录为Anna Smith,但是只修改符合条件的第一条记录!" << std::endl;  
  270.     typedef employee_set::index<name>::type employee_set_by_name;  
  271.     //因为要修改iter所指向的数据所以下面的语句不能使用const修饰  
  272.     employee_set_by_name& name_index = table.get<name>();  
  273.   
  274.     employee_set_by_name::iterator it = name_index.find(L"Anna Jones");  
  275.     //修改key的值,key必须可以读写  
  276.     name_index.modify_key(it, boost::lambda::_1 = L"Anna Smith");  
  277.     print_out_by_id(table);  
  278.   
  279.     std::wcout << L"把名为Anna Smith的第一条记录修改为Anna Jones" << std::endl;  
  280.     //下面这种方法可以修改对象某个属性的值,但是需要定义change_name类  
  281.     it = name_index.find(L"Anna Smith");  
  282.     name_index.modify(it, change_name(L"Anna Jones"));  
  283.     print_out_by_id(table);  
  284.   
  285.   
  286.     {  
  287.         std::wcout << L"修改所有name==Z的记录为kagula" << std::endl;  
  288.         employee_set_by_name& viewName = table.get<name>();  
  289.         employee_set_by_name::iterator it;  
  290.         while ( (it = viewName.find(L"Z")) != viewName.end() )  
  291.         {  
  292.             viewName.modify(it, change_name(L"kagula"));  
  293.         }  
  294.         print_out_by_id(table);  
  295.   
  296.         //Warnning:  
  297.         //不要在生产环境中,使用下面的代码来修改所有符合条件的记录  
  298.         //因为如果没有上边的代码段,下面的代码只会修改第一条符合条件的记录  
  299.         //后,提早退出循环。  
  300.         std::wcout << L"修改所有name==kagula的记录为Z" << std::endl;  
  301.         viewName = table.get<name>();  
  302.         std::pair<employee_set_by_name::iterator, employee_set_by_name::iterator> p;  
  303.         p = viewName.equal_range(L"kagula");  
  304.         for (employee_set_by_name::iterator it = p.first; it != p.second; it++)  
  305.         {  
  306.             viewName.modify(it, change_name(L"Z"));  
  307.         }  
  308.         print_out_by_id(table);  
  309.     }  
  310.   
  311.   
  312.     //因为修改记录的值是通过it对象,所以可以使用select_stat介绍的知识修改指定范围的记录  
  313. }  
  314.   
  315. int _tmain(int argc, _TCHAR* argv[])  
  316. {  
  317.     //使std::wcout能够打印中文  
  318.     std::wcout.imbue(std::locale(std::locale("chs")));  
  319.   
  320.     //建立样本  
  321.     //等价SQL,Add语句的相关功能  
  322.     employee_set table;  
  323.     CreateSample(table);  
  324.   
  325.     //这里测试等价SQL,Select语句的相关功能  
  326.     select_stat(table);  
  327.       
  328.     //这里测试等价SQL,Update语句的相关功能  
  329.     update_stat(table);  
  330.   
  331.     //这里测试等价SQL,Delete语句的相关功能  
  332.     delete_stat(table);  
  333.   
  334.     //暂停,输入任意键继续  
  335.     system("pause");  
  336.     return 0;  
  337. }  

猜你喜欢

转载自blog.csdn.net/aaa123524457/article/details/80177843