代码来自:http://blog.csdn.net/whuqin/article/details/8482547

该容器能实现多列索引,挺好。

  1. #include <string>
  2. #include <iostream>
  3. #include <boost/multi_index_container.hpp>
  4. #include <boost/multi_index/member.hpp>
  5. #include <boost/multi_index/ordered_index.hpp>
  6.  
  7. using namespace boost;
  8. using namespace boost::multi_index;
  9. using namespace std;
  10. struct Employee{
  11. int id;
  12. string name;
  13. int age;
  14.  
  15. Employee(int id_,string name_,int age_):id(id_),name(name_),age(age_){}
  16.  
  17. friend ostream& operator<<(ostream& os,const Employee& e)
  18. {
  19. os<<e.id<<" "<<e.name<<" "<<e.age<<endl;
  20. return os;
  21. }
  22. };
  23.  
  24. typedef multi_index_container<
  25. Employee,
  26. indexed_by<
  27. ordered_unique<member<Employee, int, &Employee::id> >,
  28. ordered_non_unique<member<Employee, string, &Employee::name> >,
  29. ordered_non_unique<member<Employee, int, &Employee::age> >
  30. >
  31. > EmployeeContainer;
  32.  
  33. typedef EmployeeContainer::nth_index<>::type IdIndex;
  34. typedef EmployeeContainer::nth_index<>::type NameIndex;
  35. typedef EmployeeContainer::nth_index<>::type AgeIndex;
  36.  
  37. int main(){
  38. EmployeeContainer con;
  39. con.insert(Employee(,"Joe",));
  40. con.insert(Employee(,"Robert",));
  41. con.insert(Employee(,"John",));
  42.  
  43. IdIndex& ids = con.get<>();
  44. copy(ids.begin(),ids.end(), ostream_iterator<Employee>(cout));
  45. cout << endl;
  46.  
  47. NameIndex& names = con.get<>();
  48. copy(names.begin(), names.end(), ostream_iterator<Employee>(cout));
  49. cout << endl;
  50.  
  51. // names.erase(names.begin());
  52.  
  53. AgeIndex& ages = con.get<>();
  54. copy(ages.begin(), ages.end(), ostream_iterator<Employee>(cout));
  55. cout << endl;
  56.  
  57. return ;
  58. }

Boost练习程序(multi_index_container)的更多相关文章

  1. VS2013第一个应用boost的程序

    下载boost binary https://sourceforge.net/projects/boost/files/boost-binaries/1.67.0_b1/ 由于我用的是Windows1 ...

  2. 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法

    1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...

  3. Boost.Python简介

    Boost.Python简单概括:是Boost库的一部分:用来在C++代码中调用python代码以及在Python代码中调用C++代码,并且避免用户直接操作指针. 以下内容搬运自:https://wi ...

  4. windows下用vs2008和boost结合编译程序

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://co63oc.blog.51cto.com/904636/504469 win ...

  5. Boost程序库完全开发指南——深入C++“准”标准库(第3版)

    内容简介  · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...

  6. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  7. Boost 和 Boost.Build 的设置

    问题: 安装编译完 Boost 后,如果不设置 BOOST_ROOT 和 BOOST_BUILD_PATH 则可能导致使用 bjam 时定位到 Boost 默认的路径 /usr/share/boost ...

  8. 新手,Visual Studio 2013 配置Boost库,如何编译和选择

    QuantLib installation in VC++ 2010 and later 参考:http://quantlib.org/install/vc10.shtml 1,到官网下载最新的boo ...

  9. 【视频开发】【计算机视觉】doppia编译之二:boost安装

    编译安装boost库的方法大部分都是来自http://www.linuxidc.com/Linux/2013-07/87573.htm这篇文章,这里我用自己的语言重新组织,稍作修改和补充,最主要是方便 ...

随机推荐

  1. [转载]能不能同时用static和const修饰类的成员函数?

    题目(一):我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量).请问:能不能同时用static和con ...

  2. pip 安装命令

    pip官网文档 https://pip.pypa.io/en/latest/reference/pip.html 若没有将c:\Python27\Scripts加入到path环境变量,可以在c:\Py ...

  3. 《Java语言程序设计》上机实验

    实验一   Java环境演练   [目的] ①安装并配置Java运行开发环境: ②掌握开发Java应用程序的3个步骤:编写源文件.编译源文件和运行应用程序: ③学习同时编译多个Java源文件. [内容 ...

  4. 为Linux服务器设置静态IP的方法

    这里以CentOS 7系列为例设置静态IP,原来RedHat系列的Linux发行版可以通过setup工具方便的设置静态IP,但是在版本7之后setup工具的功能就逐渐减弱了,所以这时候采用修改配置文件 ...

  5. Linux基础命令总结

    1.pwd  查看当前工作目录 2.ls [目录] 列出指定目录下的所有文件,使用 ls -l 或者 ll 列出文件详细列表包括权限.大小等文件默认大小以字节B为单位,目录大小为4096B  ls - ...

  6. 1.SpringMVC的简介和环境搭建

    SpringMVC的简介: SpringMVC 和 Struts一样是一个MVC框架,和Spring无缝连接,和struts2类似, Spring MVC属于SpringFrameWork的后续产品, ...

  7. C#在类中用调用Form的方法

    class 你的类 { private Form1 frm; //构造函数 public 你的类( Form1 form) { frm = form; } //调用form方法 private voi ...

  8. ajax传值方式为数组

    js: function responseJson1(){    var array=[1001,1002];    var str="";        //获取table对象  ...

  9. July 4th, Week 28th Monday, 2016

    Goals determine what you are going to be. 你的目标决定你将成为怎样的人. What are your goals? What kind of people y ...

  10. CABasicAnimation 按home键后台之后,再切回来动画就停止了

    解决方法: 1. CABasicAnimation *thisAnimation = [CABasicAnimtaion animationWithKeyPath:@"transform.r ...